Skip to main content

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonschema_gentypes-1.3.2.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

jsonschema_gentypes-1.3.2-py3-none-any.whl (18.6 kB view details)

Uploaded Python 3

File details

Details for the file jsonschema_gentypes-1.3.2.tar.gz.

File metadata

  • Download URL: jsonschema_gentypes-1.3.2.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.10

File hashes

Hashes for jsonschema_gentypes-1.3.2.tar.gz
Algorithm Hash digest
SHA256 2419c33a35b7fec85b08037c43d2d058d80cee796348f67a3bc2ca5b5fdbc39e
MD5 ed687a8cd360a81ae4843d63178e844c
BLAKE2b-256 d41c599d6863f65ef3d2d7903779aafb931b557519bf74df3bd9b2f22a9aa385

See more details on using hashes here.

Provenance

File details

Details for the file jsonschema_gentypes-1.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonschema_gentypes-1.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6753c04ce64632e9be1f2004e25ced751bb5100a90963ab6adde23ac654fd4a3
MD5 dd62a73fa679292c0f3be9b37aeed98e
BLAKE2b-256 4f446e24a08fd023fdd09aa59b571930505465871755f625af5113972fab1fb0

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page