Skip to main content

JSON Schema Validation within Jinja2 Templates

Tests Python versions PyPI Formatter & Linter: Ruff Type-checker: mypy

A Jinja2 extension providing a Jinja2 filter and a Jinja2 test for validating data against a JSON/YAML schema within Jinja2 templates.

Installation

  • With pip:

    pip install jinja2-jsonschema
    
    # ... or with YAML support
    pip install jinja2-jsonschema[yaml]
    
  • With poetry:

    poetry add jinja2-jsonschema
    
    # ... or with YAML support
    poetry add jinja2-jsonschema -E yaml
    
  • With pdm:

    pdm add jinja2-jsonschema
    
    # ... or with YAML support
    pdm add jinja2-jsonschema[yaml]
    
  • With pipx (injected into the pipx-managed virtual env of a package):

    pipx inject PACKAGE jinja2-jsonschema
    
    # ... or with YAML support
    pipx inject PACKAGE jinja2-jsonschema[yaml]
    

Usage

The extension provides:

  • A Jinja2 filter which receives a schema file path or schema object as input and returns a jsonschema.ValidationError object when validation fails and an empty string ("") otherwise.
  • A Jinja2 test which receives a schema file path or schema object as input and returns False when validation fails and True otherwise.

The JSON Schema dialect is inferred from the $schema field in the JSON Schema document and, when omitted, defaults to the latest dialect supported by the installed jsonschema library. Both local and remote schemas are supported including schema references and JSON Pointers.

Local schema files are loaded via a Jinja2 loader in which case configuring the Jinja2 environment with a loader is mandatory.

Some example usage of the JSON Schema validation filter and test is this:

from jinja2 import Environment
from jinja2 import FileSystemLoader


env = Environment(
    # Register a loader (only necessary when using local schema files).
    loader=FileSystemLoader("/path/to/templates"),
    # Register the extension.
    extensions=["jinja2_jsonschema.JsonSchemaExtension"],
)

# Example using an inline schema object.
template = env.from_string("{{ age | jsonschema({'type': 'integer', 'minimum': 0}) }}")
template.render(age=30)  # OK
template.render(age=-1)  # ERROR
template = env.from_string("{{ age is jsonschema({'type': 'integer', 'minimum': 0}) }}")
template.render(age=30)  # --> `True`
template.render(age=-1)  # --> `False`

# Example using a local schema file.
template = env.from_string("{{ age | jsonschema('age.json') }}")
template.render(age=30)  # OK
template.render(age=-1)  # ERROR
template = env.from_string("{{ age is jsonschema('age.json') }}")
template.render(age=30)  # --> `True`
template.render(age=-1)  # --> `False`

# Example using a remote schema file.
template = env.from_string("{{ age | jsonschema('https://example.com/age.json') }}")
template.render(age=30)  # OK
template.render(age=-1)  # ERROR
template = env.from_string("{{ age is jsonschema('https://example.com/age.json') }}")
template.render(age=30)  # --> `True`
template.render(age=-1)  # --> `False`

Usage with Copier

The extension integrates nicely with Copier, e.g. for validating complex JSON/YAML answers in the Copier questionnaire. For this, add the extension as a Jinja2 extension in copier.yml and use the Jinja2 filter in the validator field of a Copier question. For instance:

_jinja_extensions:
  - jinja2_jsonschema.JsonSchemaExtension

complex_question:
  type: json # or `yaml`
  validator: "{{ complex_question | jsonschema('schemas/complex.json') }}"

In this example, a local schema file schemas/complex.json is used whose path is relative to the template root. To prevent copying schema files to the generated project, they should be either excluded

+_exclude:
+  - schemas/
 _jinja_extensions:
   - jinja2_jsonschema.JsonSchemaExtension

or the project template should be located in a subdirectory such as template/:

+_subdirectory_: template
 _jinja_extensions:
   - jinja2_jsonschema.JsonSchemaExtension

Finally, template consumers need to install the extension along with Copier. For instance with pipx:

pipx install copier
pipx inject copier jinja2-jsonschema

Contributions

Contributions are always welcome via filing issues or submitting pull requests. Please check the contribution guide for more details.

Metadata

Release files for jinja2-jsonschema 0.5.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 jinja2-jsonschema 0.5.0
File Size Uploaded Provenance Actions
jinja2_jsonschema-0.5.0.tar.gz 6.8 kB With provenance Details for jinja2_jsonschema-0.5.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for jinja2-jsonschema 0.5.0
File Interpreter ABI Platform Actions
jinja2_jsonschema-0.5.0-py3-none-any.whl Python 3 none any Details for jinja2_jsonschema-0.5.0-py3-none-any.whl

Total release size: 13.5 kB

Release files / jinja2_jsonschema-0.5.0.tar.gz

Download URL jinja2_jsonschema-0.5.0.tar.gz
Size 6.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c6ec9adf37d8ff9c3573c96e682efd151fc99b2c2772cfbceeaaf0a35d877399
BLAKE2b-256 checksum
How to use checksums
6ea53886097ae1fa13dec8ebdd55c5f1839bc6a6778137689a225d4c0b4e4b91
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 Oct 30, 2026.

Transparency log

Release files / jinja2_jsonschema-0.5.0-py3-none-any.whl

Download URL jinja2_jsonschema-0.5.0-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c17583e68544289461e60fd48bd57b18d83cb4a14bb46526b6b897b6fad06d29
BLAKE2b-256 checksum
How to use checksums
cadde916cf1424186aa5d2350f972cd1ab0de7080d4a2500a30d23632f498769
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 Oct 30, 2026.

Transparency log

Security for jinja2-jsonschema 0.5.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

All 2 files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for jinja2_jsonschema-0.5.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 Oct 30, 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.

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