The PyPI package jsontron adds the Jsontron keywords to jsonschema. JsontronValidator is jsonschema's Draft202012Validator with rules, assert, report and let, used like any jsonschema validator class, and their failures are ordinary ValidationErrors. jq is evaluated by devlooped-jqlite, in pure Python.
jsontron.SPEC_VERSION says so. It ships type information (py.typed).$ python -m pip install jsontron
from jsontron import JsontronValidator
schema = {
"type": "object",
"required": ["orders", "policy"],
"rules": [
{
"context": ".orders[] | select(.total > 1000)",
"asserts": [
{
"test": ".discount >= $root.policy.minDiscount",
"message": "order \\(.id): discount \\(.discount) is below policy",
}
],
}
],
}
orders = {
"policy": {"minDiscount": 0.1},
"orders": [
{"id": "A-1", "total": 2500, "discount": 0.05},
{"id": "A-2", "total": 80, "discount": 0},
],
}
JsontronValidator.check_schema(schema)
validator = JsontronValidator(schema)
print(validator.is_valid(orders)) # False
for error in validator.iter_errors(orders):
print(f"{error.json_path} {error.validator}: {error.message}")
False
$ rules: order A-1: discount 0.05 is below policy
The rule selects the orders over 1000, so only A-1 is tested. The failure is reported at the location of the instance the schema object applies to, here the document itself, under the keyword rules: see Validation results.
| Page | What it covers |
|---|---|
| Building schemas | JsontronValidator, extend, check_schema and SchemaError |
| Validation results | the ValidationErrors of failures and of evaluation errors |
| API reference | every public name of the package |
| Package readme | the readme published with the package |
The keywords themselves are described in the guide.