Skip to main content

Tool to validate files against a JSON Schema

Project description

JSON Schema validator

Tools to generate Python types based on TypedDict from a JSON schema

Quick start

install:

python3 -m pip install jsonschema-validator-new

Convert a JSON schema to a Python file contains the types:

jsonschema-validator --help

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_validator
import json

schema_data = pkgutil.get_data("<package>", "schema.json")
schema = json.loads(jsonschema_validator)
with open(filename) as data_file:
    yaml = ruamel.yaml.YAML()  # type: ignore
    data = yaml.load(data_file)
errors, _ = jsonschema_validator.validate(filename, data, schema)
if errors:
    print("\n".join(errors))
    sys.exit(1)

Pre-commit hooks

This project provides pre-commit hooks to automatically generate the files.

repos:
  - repo: https://github.com/camptocamp/jsonschema-validator
    rev: <version> # Use the ref you want to point at
    hools:
      - id: jsonschema-validator
        files: |
          (?x)^(
              ...
          )$

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


Download files

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

Source Distribution

jsonschema_validator_new-0.3.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file jsonschema_validator_new-0.3.2.tar.gz.

File metadata

File hashes

Hashes for jsonschema_validator_new-0.3.2.tar.gz
Algorithm Hash digest
SHA256 622a93406d7ebc14ab40f54f83688ccbf0ee37acf81b9543c82914c5077555ba
MD5 dc77090f417f90bb9241a9ce835d1e11
BLAKE2b-256 c6d5ed89f08eaa4bf821559a6b2ad79ef32c1186b8a950507e7bd4b1508d5688

See more details on using hashes here.

File details

Details for the file jsonschema_validator_new-0.3.2-py3-none-any.whl.

File metadata

File hashes

Hashes for jsonschema_validator_new-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1b18a2b2a6115c00ec879d2dc6f99fc657f1086f62b0abef1484165ff19b1b8d
MD5 24d6c000a28c379bc201707a456c8b3b
BLAKE2b-256 d093d1810ebd3594c3cadbf6db26a16425e4b6247fa517f9a7f91bb869d9a6cf

See more details on using hashes here.

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