Tool to generate Python types based on TypedDict from a JSON Schema
Project description
JSON Schema generate Python types
Tools to generate Python types based on TypedDict from a JSON schema
Quick start
install:
python3 -m pip install --user jsonschema-gentypes
Convert a JSON schema to a Python file contains the types:
jsonschema-gentypes --json-schema=<JSON schema> --python=<destination Python file>
Docker
You can also run it with Docker:
docker run --rm --user=$(id --user) --volume=$(pwd):/src camptocamp/jsonschema-gentypes
Config file
You can also write a config file named jsonschema-gentypes.yaml
with:
headers: >
# Automatically generated file from a JSON schema
# Used to correctly format the generated file
callbacks:
- - black
- - isort
generate:
- # JSON schema file path
source: jsonschema_gentypes/schema.json
# Python file path
destination: jsonschema_gentypes/configuration.py
# The name of the root element
root_name: Config
# Argument passed to the API
api_arguments:
additional_properties: Only explicit
# Rename an element
name_mapping: {}
And just run:
jsonschema-gentypes
Validation
This package also provide some validations features for YAML file based on jsonschema
.
Additional features:
- Have the line and columns number in the errors, it the file is loaded with
ruamel.yaml
. - Fill with the default provides in the JSON schema, disabled by default because it have some issue with
OneOf
andAnyOf
.
import ruamel.yaml
import pkgutil
import jsonschema_gentypes.validate
schema_data = pkgutil.get_data("jsonschema_gentypes", "schema.json")
with open(filename) as data_file:
yaml = ruamel.yaml.YAML() # type: ignore
data = yaml.load(data_file)
errors, data = jsonschema_gentypes.validate.validate(filename, data, schema, default)
if errors:
print("\n".join(errors))
sys.exit(1)
Limitations
Requires Python 3.8
See the issues with label "limitation".
Contribute
The code should be formatted with isort
and black
.
The code should be typed.
The prospector
tests should pass.
The code should be tested with pytests
.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for jsonschema_gentypes-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a3f44f53aa6344538b0ec96c3c2c411e6c9951b40713be50ccc6139e7c16688f |
|
MD5 | fd39fba9a13141044c19e53541dc7538 |
|
BLAKE2b-256 | d4b6d76db78d2ecee422aa3093e48e7c2037b6f7f4aa569e8eba0eed777f63b8 |