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: {}
# The minimum Python version that the code should support. By default the
# currently executing Python version is chosen. Note that the output
# may require typing_extensions to be installed.
python_version: '3.11'
And just run:
jsonschema-gentypes
Default
The default values are exported in the Python file, then you can do something like that:
value_with_default = my_object.get('field_name', my_schema.FIELD_DEFAULT)
Validation
This package also provide some validations features for YAML file based on jsonschema
.
Additional features:
- Obtain the line and columns number in the errors, if the file is loaded with
ruamel.yaml
. - Export the default provided in the JSON schema.
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)
if errors:
print("\n".join(errors))
sys.exit(1)
The filling of the default value is deprecated because it can produce quite peculiar things, see also the jsonschema documentation.
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 Distribution
Built Distribution
Hashes for jsonschema_gentypes-1.5.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d53f22ea850023ca3af60b9a9425f2c75bb83faffa5463e18e78c1c1a6c7a0c |
|
MD5 | 720626b0797bfc378da5916cea9038c8 |
|
BLAKE2b-256 | 73bdc8c9ea47aafb818849648745ef6cb07557bbf8466d201fed3034a7793daf |
Hashes for jsonschema_gentypes-1.5.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4cb769bfd206610fe2b673daab015cf7c3df0dfddf903dba332a08b691469426 |
|
MD5 | ee9b6f88374db36d4d602043aecd2e49 |
|
BLAKE2b-256 | 5b2b084e81a15e1d080147548ff28a8b611720d2d0f0137ce0efc25b639ad64e |