JSON Schema Tool
JSON Schema Tool is a python implementation of JSON Schema, draft 2020-12. It offers various additional features commonly not found in other libraries
Obviously, the core of JSON Schema Tool is the validation of JSON documents. This can be done as follows:
from json_schema_tool import parse_schema
schema = {
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'type': 'object',
'properties': {
'foo': {
'type': 'string'
}
}
}
validator = parse_schema(schema)
result = validator.validate({"foo": "bar"})
# result.ok == True
result = validator.validate("invalid")
# result.ok == False
Installation
You can install JSON Schema Tool via pip:
python -m pip install json_schema_tool
Features
Additional Keywords
Json Schema Tool supports OpenAPI's discriminator keyword for improved modelling of polymorphism:
schema = {
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'oneOf': [
{'$ref': '#/$defs/Cat'},
{'$ref': '#/$defs/Dog'},
],
'$defs': {
'Cat': {
'properties': {'sound': {'const': 'meow'}}
},
'Dog': {
'properties': {'sound': {'const': 'woof'}}
}
},
'discriminator': {
'propertyName': 'type'
}
}
result = validator.validate({'type': 'Cat', 'sound': '?'})
# result.ok == False
Using the discriminator type, Json Schema Tool knows which reference to check and will only return an error for the Cat type (and will not check Dog).
For more information, see https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/.
Schema Coverage Measurement
You can use coverage to assess the completeness of your test data. Schema coverage works on the keyword level, i.e., JsonSchema Tool checks, how many constraints have been actually checked during instance validation:
from json_schema_tool import coverage, parse_schema
schema = {
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'type': 'object',
'properties': {
'foo': {
'type': 'string'
}
}
}
validator = parse_schema(schema)
cov = coverage.SchemaCoverage(validator)
result = validator.validate({})
cov.update(result)
print(cov.coverage())
# 0.3
result = validator.validate({"foo": "bar"})
cov.update(result)
print(cov.coverage())
# 1.0
with open("schema-coverage.html", "w") as f:
cov.render_coverage(f)
Type Inference
Given a validator, you can use it to query the types of the schema. This even works for complex and composed schemas:
from json_schema_tool import parse_schema
schema = {
'$schema': 'https://json-schema.org/draft/2020-12/schema',
'anyOf': [
{"type": "object"},
{"const": "foo"}
]
}
validator = parse_schema(schema)
print(validator.get_types())
# {'object', 'string'}
Validation Performance
You can drastically increase validation performance by using short circuit evaluation (SCE). By using SCE, evaluation terminates as soon as the first error in the JSON instance is found. For example, an allOf does not visit all sub schemas, if the first sub-schema already fails. You can activate SCE as follows:
from json_schema_tool import schema
# use parse_schema to build your validator...
config = schema.ValidationConfig(short_circuit_evaluation=True)
result = validator.validate({"foo": "bar"}, config)
Please note, that SCE does not work together with coverage measurement.
Metadata
Release files for json-schema-tool 0.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| json_schema_tool-0.4.0.tar.gz | 18.8 kB | Without provenance | Details for json_schema_tool-0.4.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json_schema_tool-0.4.0-py3-none-any.whl | Python 3 | none | any | Details for json_schema_tool-0.4.0-py3-none-any.whl |
Total release size: 35.2 kB
Release files / json_schema_tool-0.4.0.tar.gz
| Download URL | json_schema_tool-0.4.0.tar.gz |
|---|---|
| Size | 18.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2b90109a44ae3fe6237e8da9ff313aff2eb48a4ad367ca1734e9860110f9ffb7
|
|
BLAKE2b-256 checksum How to use checksums |
7b8065232d7dea33c876c17e46cb61ced5a1c3880d0f7cf160c4b2b94160f6f1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Release files / json_schema_tool-0.4.0-py3-none-any.whl
| Download URL | json_schema_tool-0.4.0-py3-none-any.whl |
|---|---|
| Size | 16.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
140414cfffd270e3b8f0eef22213c122c60cb5a112edf9596f5816b3aff7ad59
|
|
BLAKE2b-256 checksum How to use checksums |
cd0cb6e940d93ffd6b50bad262d20f7eaeaa1dc9f4e28a84574d52daed9fed5c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.19
|
Security for json-schema-tool 0.4.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.