{"id":15012983,"url":"https://github.com/copier-org/jinja2-jsonschema","last_synced_at":"2025-07-03T18:33:57.986Z","repository":{"id":166407586,"uuid":"638770687","full_name":"copier-org/jinja2-jsonschema","owner":"copier-org","description":"JSON schema validation within Jinja2 templates","archived":false,"fork":false,"pushed_at":"2025-07-02T17:34:11.000Z","size":580,"stargazers_count":10,"open_issues_count":2,"forks_count":1,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-07-02T18:36:43.594Z","etag":null,"topics":["jinja","jinja2","jinja2-extension","json","jsonschema","yaml"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/copier-org.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"github":["Yajo"]}},"created_at":"2023-05-10T04:21:58.000Z","updated_at":"2025-07-02T17:34:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"88439df7-ee01-4ebc-8bd6-67dfca279fa8","html_url":"https://github.com/copier-org/jinja2-jsonschema","commit_stats":{"total_commits":130,"total_committers":3,"mean_commits":"43.333333333333336","dds":0.2384615384615385,"last_synced_commit":"965fde3d1cb1006f4b4e4abb02ce53f8dc411e9d"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/copier-org/jinja2-jsonschema","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/copier-org%2Fjinja2-jsonschema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/copier-org%2Fjinja2-jsonschema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/copier-org%2Fjinja2-jsonschema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/copier-org%2Fjinja2-jsonschema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/copier-org","download_url":"https://codeload.github.com/copier-org/jinja2-jsonschema/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/copier-org%2Fjinja2-jsonschema/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263379600,"owners_count":23457876,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["jinja","jinja2","jinja2-extension","json","jsonschema","yaml"],"created_at":"2024-09-24T19:43:33.529Z","updated_at":"2025-07-03T18:33:57.947Z","avatar_url":"https://github.com/copier-org.png","language":"Python","funding_links":["https://github.com/sponsors/Yajo"],"categories":[],"sub_categories":[],"readme":"# JSON Schema Validation within Jinja2 Templates\n\n[![Tests](https://img.shields.io/github/actions/workflow/status/copier-org/jinja2-jsonschema/tests.yml?branch=main\u0026label=Tests\u0026labelColor=333\u0026logo=github\u0026style=flat-square)](https://github.com/copier-org/jinja2-jsonschema/actions?query=branch%3Amain)\n[![Python versions](https://img.shields.io/pypi/pyversions/jinja2-jsonschema?label=Python\u0026logo=python\u0026logoColor=%23959DA5\u0026style=flat-square)](https://pypi.org/project/jinja2-jsonschema)\n[![PyPI](https://img.shields.io/pypi/v/jinja2-jsonschema?label=PyPI\u0026logo=pypi\u0026logoColor=%23959DA5\u0026style=flat-square)](https://pypi.org/project/jinja2-jsonschema)\n[![Formatter \u0026 Linter: Ruff](https://img.shields.io/badge/-Ruff-261230.svg?labelColor=grey\u0026logo=ruff\u0026logoColor=D7FF64\u0026style=flat-square)](https://github.com/charliermarsh/ruff)\n[![Type-checker: mypy](https://img.shields.io/badge/mypy-strict-2A6DB2.svg?style=flat-square)](http://mypy-lang.org)\n\nA [Jinja2 extension][jinja-extensions] providing a [Jinja2 filter][jinja-filter] and a [Jinja2 test][jinja-test] for validating data against a JSON/YAML schema within [Jinja2][jinja] templates.\n\n## Installation\n\n* With [`pip`](https://pip.pypa.io):\n\n    ```shell\n    pip install jinja2-jsonschema\n\n    # ... or with YAML support\n    pip install jinja2-jsonschema[yaml]\n    ```\n\n* With [`poetry`][poetry]:\n\n    ```shell\n    poetry add jinja2-jsonschema\n\n    # ... or with YAML support\n    poetry add jinja2-jsonschema -E yaml\n    ```\n\n* With [`pdm`][pdm]:\n\n    ```shell\n    pdm add jinja2-jsonschema\n\n    # ... or with YAML support\n    pdm add jinja2-jsonschema[yaml]\n    ```\n\n* With [`pipx`][pipx] (injected into the `pipx`-managed virtual env of a package):\n\n    ```shell\n    pipx inject PACKAGE jinja2-jsonschema\n\n    # ... or with YAML support\n    pipx inject PACKAGE jinja2-jsonschema[yaml]\n    ```\n\n## Usage\n\nThe extension provides:\n\n* A Jinja2 filter which receives a schema file path or schema object as input and returns a [`jsonschema.ValidationError`][python-jsonschema-validationerror] object when validation fails and an empty string (`\"\"`) otherwise.\n* A Jinja2 test which receives a schema file path or schema object as input and returns `False` when validation fails and `True` otherwise.\n\nThe [JSON Schema dialect][jsonschema-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][python-jsonschema-features]. Both local and remote schemas are supported including [schema references][jsonschema-ref] and [JSON Pointers][jsonschema-jsonpointer].\n\nLocal schema files are loaded via a [Jinja2 loader](https://jinja.palletsprojects.com/en/latest/api/#loaders) in which case configuring the Jinja2 environment with a loader is mandatory.\n\nSome example usage of the JSON Schema validation filter and test is this:\n\n```python\nfrom jinja2 import Environment\nfrom jinja2 import FileSystemLoader\n\n\nenv = Environment(\n    # Register a loader (only necessary when using local schema files).\n    loader=FileSystemLoader(\"/path/to/templates\"),\n    # Register the extension.\n    extensions=[\"jinja2_jsonschema.JsonSchemaExtension\"],\n)\n\n# Example using an inline schema object.\ntemplate = env.from_string(\"{{ age | jsonschema({'type': 'integer', 'minimum': 0}) }}\")\ntemplate.render(age=30)  # OK\ntemplate.render(age=-1)  # ERROR\ntemplate = env.from_string(\"{{ age is jsonschema({'type': 'integer', 'minimum': 0}) }}\")\ntemplate.render(age=30)  # --\u003e `True`\ntemplate.render(age=-1)  # --\u003e `False`\n\n# Example using a local schema file.\ntemplate = env.from_string(\"{{ age | jsonschema('age.json') }}\")\ntemplate.render(age=30)  # OK\ntemplate.render(age=-1)  # ERROR\ntemplate = env.from_string(\"{{ age is jsonschema('age.json') }}\")\ntemplate.render(age=30)  # --\u003e `True`\ntemplate.render(age=-1)  # --\u003e `False`\n\n# Example using a remote schema file.\ntemplate = env.from_string(\"{{ age | jsonschema('https://example.com/age.json') }}\")\ntemplate.render(age=30)  # OK\ntemplate.render(age=-1)  # ERROR\ntemplate = env.from_string(\"{{ age is jsonschema('https://example.com/age.json') }}\")\ntemplate.render(age=30)  # --\u003e `True`\ntemplate.render(age=-1)  # --\u003e `False`\n```\n\n## Usage with Copier\n\nThe extension integrates nicely with [Copier][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`][copier-jinja-extensions] and use the Jinja2 filter in the `validator` field of a Copier [question][copier-questions]. For instance:\n\n```yaml\n_jinja_extensions:\n  - jinja2_jsonschema.JsonSchemaExtension\n\ncomplex_question:\n  type: json # or `yaml`\n  validator: \"{{ complex_question | jsonschema('schemas/complex.json') }}\"\n```\n\nIn 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][copier-exclude]\n\n```diff\n+_exclude:\n+  - schemas/\n _jinja_extensions:\n   - jinja2_jsonschema.JsonSchemaExtension\n```\n\nor the project template should be located in a [subdirectory][copier-subdirectory] such as `template/`:\n\n```diff\n+_subdirectory_: template\n _jinja_extensions:\n   - jinja2_jsonschema.JsonSchemaExtension\n```\n\nFinally, template consumers need to install the extension along with Copier. For instance with `pipx`:\n\n```shell\npipx install copier\npipx inject copier jinja2-jsonschema\n```\n\n## Contributions\n\nContributions are always welcome via filing [issues](https://github.com/copier-org/jinja2-jsonschema/issues) or submitting [pull requests](https://github.com/copier-org/jinja2-jsonschema/pulls). Please check the [contribution guide][contribution-guide] for more details.\n\n[contribution-guide]: https://github.com/copier-org/jinja2-jsonschema/blob/main/CONTRIBUTING.md\n[copier]: https://github.com/copier-org/copier\n[copier-exclude]: https://copier.readthedocs.io/en/stable/configuring/#exclude\n[copier-jinja-extensions]: https://copier.readthedocs.io/en/stable/configuring/#jinja_extensions\n[copier-questions]: https://copier.readthedocs.io/en/stable/configuring/#questions\n[copier-subdirectory]: https://copier.readthedocs.io/en/stable/configuring/#subdirectory\n[jinja]: https://jinja.palletsprojects.com\n[jinja-extensions]: https://jinja.palletsprojects.com/en/latest/extensions/\n[jinja-filter]: https://jinja.palletsprojects.com/en/latest/templates/#filters\n[jinja-test]: https://jinja.palletsprojects.com/en/latest/templates/#tests\n[jsonschema]: https://json-schema.org\n[jsonschema-dialect]: https://json-schema.org/understanding-json-schema/reference/schema.html#schema\n[jsonschema-ref]: https://json-schema.org/understanding-json-schema/structuring.html#ref\n[jsonschema-jsonpointer]: https://json-schema.org/understanding-json-schema/structuring.html#json-pointer\n[pdm]: https://pdm.fming.dev\n[pip]: https://pip.pypa.io\n[pipx]: https://pypa.github.io/pipx\n[poetry]: https://python-poetry.org\n[python-jsonschema-features]: https://python-jsonschema.readthedocs.io/en/stable/#features\n[python-jsonschema-validationerror]: https://python-jsonschema.readthedocs.io/en/stable/api/jsonschema/exceptions/#jsonschema.exceptions.ValidationError\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcopier-org%2Fjinja2-jsonschema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcopier-org%2Fjinja2-jsonschema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcopier-org%2Fjinja2-jsonschema/lists"}