JSON/YAML schema validation within Jinja2 templates
Project description
JSON Schema Validation within Jinja2 Templates
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 thepipx-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.ValidationErrorobject when validation fails and an empty string ("") otherwise. - A Jinja2 test which receives a schema file path or schema object as input and returns
Falsewhen validation fails andTrueotherwise.
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.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jinja2_jsonschema-0.4.0.tar.gz.
File metadata
- Download URL: jinja2_jsonschema-0.4.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
63bfd15eed523963aa48a01bfac5d2670bb4e8e7899d0d1643ebcd2142da0bf6
|
|
| MD5 |
3a672e8b4e09d53983ed942038ec505d
|
|
| BLAKE2b-256 |
7513171fc2d1cbd7f12e5d1ab70abc68c3a48922fb5c7f58956e8f440ff81c7f
|
Provenance
The following attestation bundles were made for jinja2_jsonschema-0.4.0.tar.gz:
Publisher:
release.yml on copier-org/jinja2-jsonschema
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_jsonschema-0.4.0.tar.gz -
Subject digest:
63bfd15eed523963aa48a01bfac5d2670bb4e8e7899d0d1643ebcd2142da0bf6 - Sigstore transparency entry: 711378281
- Sigstore integration time:
-
Permalink:
copier-org/jinja2-jsonschema@4ba770041863e43c5be93eba430f3c07ecde8ce3 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/copier-org
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4ba770041863e43c5be93eba430f3c07ecde8ce3 -
Trigger Event:
release
-
Statement type:
File details
Details for the file jinja2_jsonschema-0.4.0-py3-none-any.whl.
File metadata
- Download URL: jinja2_jsonschema-0.4.0-py3-none-any.whl
- Upload date:
- Size: 6.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
40fab8199ab20dac97efb362079f10321621c6004efda5ce4bf70f15678598c1
|
|
| MD5 |
e2c916ff4cf92af757745da47bd6a476
|
|
| BLAKE2b-256 |
4aa33d8c1fa649f93f40f8a01f1449d2a1e7bdc64e86c0bfd7553cc4f45b29c8
|
Provenance
The following attestation bundles were made for jinja2_jsonschema-0.4.0-py3-none-any.whl:
Publisher:
release.yml on copier-org/jinja2-jsonschema
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
jinja2_jsonschema-0.4.0-py3-none-any.whl -
Subject digest:
40fab8199ab20dac97efb362079f10321621c6004efda5ce4bf70f15678598c1 - Sigstore transparency entry: 711378373
- Sigstore integration time:
-
Permalink:
copier-org/jinja2-jsonschema@4ba770041863e43c5be93eba430f3c07ecde8ce3 -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/copier-org
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@4ba770041863e43c5be93eba430f3c07ecde8ce3 -
Trigger Event:
release
-
Statement type: