Welcome to the experimental jschon branch for OpenAPI support!
jschon is a pythonic and extensible implementation of the JSON Schema specification.
jschon was created and is maintained by Mark Jacobson. This experimental fork arises from the extensibility discussion at the jschon repository, and is being developed and proposed by JSON Schema and OpenAPI specification contributor Henry Andrews to support the OASComply project.
Please check the jschon extensibility discussion for the status of whether and in what form these changes will be incorporated into jschon proper. Unless and until that occurs, please raise issues with this fork as OASComply issues.
Features
JSON Schema validator implementation (drafts 2019-09, 2020-12)
Schema compilation and indexing
$ref loading from local and remote sources
Support for custom keywords, vocabularies and meta-schemas
Support for format validation
JSON class implementing the JSON data model
JSON Pointer (RFC 6901)
JSON Patch (RFC 6902)
Relative JSON Pointer (draft)
Installation
pip install jschon
For remote $ref support, the requests library is required. It may be installed with:
pip install jschon[requests]
Usage
Create a JSON schema:
from jschon import create_catalog, JSON, JSONSchema
create_catalog('2020-12')
demo_schema = JSONSchema({
"$id": "https://example.com/demo",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "array",
"items": {
"anyOf": [
{
"type": "string",
"description": "Cool! We got a string here!"
},
{
"type": "integer",
"description": "Hey! We got an integer here!"
}
]
}
})
Validate JSON data:
result = demo_schema.evaluate(
JSON([12, "Monkeys"])
)
Generate JSON Schema-conformant output:
>>> result.output('basic')
{
"valid": True,
"annotations": [
{
"instanceLocation": "",
"keywordLocation": "/items",
"absoluteKeywordLocation": "https://example.com/demo#/items",
"annotation": True
},
{
"instanceLocation": "/0",
"keywordLocation": "/items/anyOf/1/description",
"absoluteKeywordLocation": "https://example.com/demo#/items/anyOf/1/description",
"annotation": "Hey! We got an integer here!"
},
{
"instanceLocation": "/1",
"keywordLocation": "/items/anyOf/0/description",
"absoluteKeywordLocation": "https://example.com/demo#/items/anyOf/0/description",
"annotation": "Cool! We got a string here!"
}
]
}
Links
Documentation: https://jschon.readthedocs.io
GitHub repository: https://github.com/marksparkza/jschon
PyPI package: https://pypi.org/project/jschon
Online validator: https://jschon.dev