Skip to main content

Jsontron for Python

Schematron-style business rules for JSON Schema, with jq expressions: the rules, assert, report and let keywords of the Jsontron specification (version 0.2) for jsonschema (draft 2020-12), evaluated in-process by devlooped-jqlite (pure Python, no native extensions).

  • Structural validation stays with JSON Schema.
  • Cross-cutting rules stay in the schema, next to the data they describe.
  • jq expressions are checked when the schema is checked, and compiled once.

Quick start

from jsontron import JsontronValidator

schema = {
    "type": "object",
    "required": ["orders", "policy", "approvedCustomers"],
    "rules": [
        {
            "context": ".orders[] | select(.total > 1000)",
            "asserts": [
                {
                    "test": ".discount >= ($root.policy.minHighValueDiscount // 0.1)",
                    "message": "High-value order \\(.id) discount \\(.discount) is below policy",
                },
                {
                    "test": ".customer.id | IN($root.approvedCustomers[])",
                    "message": "Customer \\(.customer.id) is not on the approved list",
                },
            ],
        }
    ],
}

document = {
    "policy": {"minHighValueDiscount": 0.15},
    "approvedCustomers": ["acme", "globex"],
    "orders": [
        {"id": "O-1001", "total": 2500, "discount": 0.05, "customer": {"id": "initech"}},
        {"id": "O-1002", "total": 80, "discount": 0, "customer": {"id": "unknown"}},
    ],
}

JsontronValidator.check_schema(schema)  # shape and every jq expression
validator = JsontronValidator(schema)

# O-1001 fails both asserts (discount + customer).
# O-1002 is ignored by the rule (total <= 1000).
for error in validator.iter_errors(document):
    print(error.message)
# High-value order O-1001 discount 0.05 is below policy
# Customer initech is not on the approved list

print(validator.is_valid(document))  # False

JsontronValidator is jsonschema.Draft202012Validator with the four keywords, and is used like any jsonschema validator class: iter_errors, validate, is_valid, check_schema, and jsonschema.validate(document, schema, cls=JsontronValidator).

jsontron.extend(validator_class) adds the keywords to another validator class. To combine them with keywords of your own, extend last: jsontron.extend(validators.extend(Draft202012Validator, {...})). A class made with validators.extend(JsontronValidator, ...) loses what tracks the document for $root.

Behaviour

  • Each failed assert or report is one ValidationError whose message is the failure text (with its [rule-id][item-id][flag=...] prefix), at the location of the instance the schema object applies to. Its validator is the keyword that owns the rule set: rules, else assert, else report.
  • A jq runtime error in a let, context or test is one ValidationError for the rule set.
  • check_schema rejects invalid keyword values and invalid jq in any expression. A schema used without check_schema raises the same SchemaError when its invalid schema object is first applied.
  • $root is the whole document, also under properties, items, $ref and applicators.
  • A schema object is compiled once, on first use: changing it in place afterwards has no effect.

Performance

Each schema object with a rule set compiles to one jqlite expression on first use (cached), and runs it once per instance it applies to. Every run converts its input to jqlite values: the instance, and the whole document when an expression reads $root. For large arrays, prefer a rule at the parent with a context that selects the items over a rule set under items that reads $root, so the document is converted once rather than per item.

Metadata

Release files for jsontron 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsontron 0.3.0
File Size Uploaded Provenance Actions
jsontron-0.3.0.tar.gz 16.7 kB With provenance Details for jsontron-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jsontron 0.3.0
File Interpreter ABI Platform Actions
jsontron-0.3.0-py3-none-any.whl Python 3 none any Details for jsontron-0.3.0-py3-none-any.whl

Total release size: 29.3 kB

Release files / jsontron-0.3.0.tar.gz

Download URL jsontron-0.3.0.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
770da437c0d7afb9b4dbae62d3285775ac2d771e111c2d2e0ce1da4c30207394
BLAKE2b-256 checksum
How to use checksums
779e10ec9be3121f74e5fbbe1c6e32d14b39d266a6002cb4a99ff16ec88a0b8f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Release files / jsontron-0.3.0-py3-none-any.whl

Download URL jsontron-0.3.0-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7f9addbcf749b7a191ba2933fc942bd2bfd4eb885ea86d55788c5254b42f2c6b
BLAKE2b-256 checksum
How to use checksums
2da9e17224e1cc6884e31af06d9e9695ab7517fbf0c91fb8a3dd6e68dccff489
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

Security for jsontron 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for jsontron-0.3.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 1, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page