Skip to content

Tool and pre-commit that can be used to validate a file against a json schema

License

Notifications You must be signed in to change notification settings

sbrunner/jsonschema-validator

Repository files navigation

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
    hooks:
      - 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.

About

Tool and pre-commit that can be used to validate a file against a json schema

Resources

License

Security policy

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •