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".
Pre-commit hooks
This project provides pre-commit hooks to automatically generate the files.
repos:
- repo: https://github.com/camptocamp/jsonschema-gentypes
rev: <version> # Use the ref you want to point at
hools:
- id: jsonschema-gentypes
files: |
(?x)^(
jsonschema-gentypes\.yaml|
<schema_path>\.json
)$
See also the pre_commit section in the configuration to run the pre-commit just after the generation, for example with:
pre_commit:
enabled: true
arguments:
- --color=never
Contributing
Install the pre-commit hooks:
pip install pre-commit
pre-commit install --allow-missing-config
The prospector
tests should pass.
The code should be typed.
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.7.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b061757a2ce7719c450eb89324b45c14f049595b6e2b86f43b8bbaee1f51695 |
|
MD5 | bfb3f58952fe210265dd04c6664850f1 |
|
BLAKE2b-256 | 31dcfb8ff8f3bd6de2ebcc934afbb7bd48b9d0e5864d61a7a242b63cc5e1c7f7 |
Hashes for jsonschema_gentypes-1.7.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89031c6db0216d980b6fbbe6c03b0395e364469d4026e1346df4e47f9ea9b111 |
|
MD5 | 6ced436754aa854c148f8ca436b75b79 |
|
BLAKE2b-256 | 0677d7faa247947d60f5f1789d91711eca413612439e9b39ecb73927194b381e |