Package: jinja2-jsonschema
JSON Schema Validation within Jinja2 TemplatesAJinja2 extensionproviding aJinja2 filterand aJinja2 testfor validating data against a JSON/YAML schema withinJinja2templates.InstallationWithpip:pipinstalljinja2-jsonschema# ... or with YAML supportpipinstalljinja2-jsonschema[yaml]Withpoetry:poetryaddjinja2-jsonschema# ... or with YAML supportpoetryaddjinja2-jsonschema-EyamlWithpdm:pdmaddjinja2-jsonschema# ... or with YAML supportpdmaddjinja2-jsonschema[yaml]Withpipx(injected into thepipx-managed virtual env of a package):pipxinjectPACKAGEjinja2-jsonschema# ... or with YAML supportpipxinjectPACKAGEjinja2-jsonschema[yaml]UsageThe extension provides:A Jinja2 filter which receives a schema file path or schema object as input and returns ajsonschema.ValidationErrorobject when validation fails and an empty string ("") otherwise.A Jinja2 test which receives a schema file path or schema object as input and returnsFalsewhen validation fails andTrueotherwise.TheJSON Schema dialectis inferred from the$schemafield in the JSON Schema document and, when omitted, defaults to thelatest dialect supported by the installedjsonschemalibrary. Both local and remote schemas are supported includingschema referencesandJSON Pointers.Local schema files are loaded via aJinja2 loaderin which case configuring the Jinja2 environment with a loader is mandatory.Some example usage of the JSON Schema validation filter and test is this:fromjinja2importEnvironmentfromjinja2importFileSystemLoaderenv=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)# OKtemplate.render(age=-1)# ERRORtemplate=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)# OKtemplate.render(age=-1)# ERRORtemplate=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)# OKtemplate.render(age=-1)# ERRORtemplate=env.from_string("{{ age is jsonschema('https://example.com/age.json') }}")template.render(age=30)# --> `True`template.render(age=-1)# --> `False`Usage with CopierThe extension integrates nicely withCopier, e.g. for validating complex JSON/YAML answers in the Copier questionnaire. For this, add the extension as aJinja2 extension incopier.ymland use the Jinja2 filter in thevalidatorfield of a Copierquestion. For instance:_jinja_extensions:-jinja2_jsonschema.JsonSchemaExtensioncomplex_question:type:json# or `yaml`validator:"{{complex_question|jsonschema('schemas/complex.json')}}"In this example, a local schema fileschemas/complex.jsonis used whose path is relative to the template root. To prevent copying schema files to the generated project, they should be eitherexcluded+_exclude:+  - schemas/_jinja_extensions:- jinja2_jsonschema.JsonSchemaExtensionor the project template should be located in asubdirectorysuch astemplate/:+_subdirectory_: template_jinja_extensions:- jinja2_jsonschema.JsonSchemaExtensionFinally, template consumers need to install the extension along with Copier. For instance withpipx:pipxinstallcopier
pipxinjectcopierjinja2-jsonschemaContributionsContributions are always welcome via filingissuesor submittingpull requests. Please check thecontribution guidefor more details.