Package: schema2type
Schema2typeInteract with JSON and YAML content through custom Python classes.
How it works:You specify the desired or expected format of your content using aJSON SchemaorOpenAPI Specification (version 3).Schema2type creates dynamic Python types for all the (sub-)schemas described by that schema/specification.You can make use of these types from anywhere in your code to interact with the JSON or YAML content.Schema2type also generates so-calledstub files,
which allow you to make use of your IDE's auto-complete functionality for the dynamically created types
(this is arguably the most convenient feature of this module and works with both PyCharm and VS Code).JSON/YAML content can be read from a file, but it can also be content obtained through any other means, such as through
an HTTP request.ExampleIf you frequently interact with OpenAPI Specifications, you might want make use of dynamic classes based on theJSON schema that describes the format of an OpenAPI Specification(indeed, "a specification of a specification document" is rather meta, but very useful in this case).importprancefrommy_schema_classes.openapiimportRootObjectasOpenAPISpecification# resolve references in the specification with the prance moduleresolved_specification=prance.ResolvingParser('/path/to/pet_store_openapi_spec.yml').specification# create a schema-based object from the YAML contentspecification_object=OpenAPISpecification(**resolved_specification)# access information from the specification with auto-complete to guide you along the way:print(specification_object.info.title)# > Swagger Petstore - OpenAPI 3.0print(type(specification_object.components))# > <class 'schema2type.Components'>pet_schema=specification_object.components.schemas['Pet']print(type(pet_schema))# > <class 'schema2type.Schema'>print(pet_schema.required)# > ['name', 'photoUrls']CompatibilityAt the time of  writing, schema2type has been tested with schemas specified in the following formats/documents:OpenAPI Specification Version 3.0JSON Schema Draft 4DisclaimerThis module is currently in development. At the moment, it can handle most, but not all situations when it comes to
parsing your JSON schema or OpenAPI specification. Use at your own risk of running into any potential errors. Support
foroneOf,allOf, andanyOfschemas is still limited.How to installSchema2type is available from PyPI, and can be installed via pip:pipinstallschema2typeUsageFirst, generate the stub files based on your schema or specification:mkdir"/path/to/my_package/my_module/schema_classes"schema2typegen-stubs--out"/path/to/my_package/my_module/schema_classes"--name"my_schema_name""/path/to/my_package/a_schema.yml"json_schemaThen, import the schema based classes from within your module wherever you need them:frommy_package.schema_classes.my_schemaimport*LicenseCopyright 2021 Mokkit OyLicensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.