Name | json-schema-tool JSON |
Version |
0.4.0
JSON |
| download |
home_page | None |
Summary | JSON Schema validation with various additional features |
upload_time | 2024-07-19 10:31:24 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.6 |
license | MIT License Copyright (c) 2023 Institut für Automation und Kommunikation e.V. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
python
json
json-schema
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# JSON Schema Tool
[](https://github.com/ifak/json_schema_tool/actions/workflows/check.yml)
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:
```python
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:
```sh
python -m pip install json_schema_tool
```
## Features
### Additional Keywords
Json Schema Tool supports OpenAPI's `discriminator` keyword for improved modelling of polymorphism:
```python
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:
```python
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:
```python
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:
```python
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "json-schema-tool",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.6",
"maintainer_email": null,
"keywords": "python, json, json-schema",
"author": null,
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/7b/80/65232d7dea33c876c17e46cb61ced5a1c3880d0f7cf160c4b2b94160f6f1/json_schema_tool-0.4.0.tar.gz",
"platform": null,
"description": "# JSON Schema Tool\n\n[](https://github.com/ifak/json_schema_tool/actions/workflows/check.yml)\n\nJSON Schema Tool is a python implementation of JSON Schema, draft 2020-12.\nIt offers various additional features commonly not found in other libraries\n\nObviously, the core of JSON Schema Tool is the validation of JSON documents.\nThis can be done as follows:\n\n```python\nfrom json_schema_tool import parse_schema\n\nschema = {\n '$schema': 'https://json-schema.org/draft/2020-12/schema',\n 'type': 'object',\n 'properties': {\n 'foo': {\n 'type': 'string'\n }\n }\n}\n\nvalidator = parse_schema(schema)\n\nresult = validator.validate({\"foo\": \"bar\"})\n# result.ok == True\n\nresult = validator.validate(\"invalid\")\n# result.ok == False\n\n```\n\n## Installation\n\nYou can install JSON Schema Tool via pip:\n\n```sh\npython -m pip install json_schema_tool\n```\n\n## Features\n\n### Additional Keywords\n\nJson Schema Tool supports OpenAPI's `discriminator` keyword for improved modelling of polymorphism:\n\n```python\nschema = {\n '$schema': 'https://json-schema.org/draft/2020-12/schema',\n 'oneOf': [\n {'$ref': '#/$defs/Cat'},\n {'$ref': '#/$defs/Dog'},\n ],\n '$defs': {\n 'Cat': {\n 'properties': {'sound': {'const': 'meow'}}\n },\n 'Dog': {\n 'properties': {'sound': {'const': 'woof'}}\n }\n },\n 'discriminator': {\n 'propertyName': 'type'\n }\n}\nresult = validator.validate({'type': 'Cat', 'sound': '?'})\n# result.ok == False\n```\n\nUsing 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`).\nFor more information, see https://swagger.io/docs/specification/data-models/inheritance-and-polymorphism/.\n\n### Schema Coverage Measurement\nYou can use coverage to assess the completeness of your test data.\nSchema coverage works on the keyword level, i.e., JsonSchema Tool checks, how many constraints have been actually checked during instance validation:\n\n```python\nfrom json_schema_tool import coverage, parse_schema\nschema = {\n '$schema': 'https://json-schema.org/draft/2020-12/schema',\n 'type': 'object',\n 'properties': {\n 'foo': {\n 'type': 'string'\n }\n }\n}\n\nvalidator = parse_schema(schema)\ncov = coverage.SchemaCoverage(validator)\n\nresult = validator.validate({})\ncov.update(result)\nprint(cov.coverage())\n# 0.3\n\nresult = validator.validate({\"foo\": \"bar\"})\ncov.update(result)\nprint(cov.coverage())\n# 1.0\n\nwith open(\"schema-coverage.html\", \"w\") as f:\n cov.render_coverage(f)\n```\n\n### Type Inference\nGiven a validator, you can use it to query the types of the schema.\nThis even works for complex and composed schemas:\n```python\nfrom json_schema_tool import parse_schema\nschema = {\n '$schema': 'https://json-schema.org/draft/2020-12/schema',\n 'anyOf': [\n {\"type\": \"object\"},\n {\"const\": \"foo\"}\n ]\n}\nvalidator = parse_schema(schema)\nprint(validator.get_types())\n# {'object', 'string'}\n```\n\n### Validation Performance\nYou can drastically increase validation performance by using short circuit evaluation (SCE).\nBy using SCE, evaluation terminates as soon as the first error in the JSON instance is found.\nFor example, an allOf does not visit all sub schemas, if the first sub-schema already fails.\nYou can activate SCE as follows:\n\n```python\nfrom json_schema_tool import schema\n\n# use parse_schema to build your validator...\n\nconfig = schema.ValidationConfig(short_circuit_evaluation=True)\nresult = validator.validate({\"foo\": \"bar\"}, config)\n```\nPlease note, that SCE does not work together with coverage measurement.\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2023 Institut f\u00fcr Automation und Kommunikation e.V. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
"summary": "JSON Schema validation with various additional features",
"version": "0.4.0",
"project_urls": {
"Homepage": "https://github.com/ifak/json_schema_tool"
},
"split_keywords": [
"python",
" json",
" json-schema"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "cd0cb6e940d93ffd6b50bad262d20f7eaeaa1dc9f4e28a84574d52daed9fed5c",
"md5": "c98df7261ef6a29538ecce23b7c126dc",
"sha256": "140414cfffd270e3b8f0eef22213c122c60cb5a112edf9596f5816b3aff7ad59"
},
"downloads": -1,
"filename": "json_schema_tool-0.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "c98df7261ef6a29538ecce23b7c126dc",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6",
"size": 16487,
"upload_time": "2024-07-19T10:31:21",
"upload_time_iso_8601": "2024-07-19T10:31:21.613769Z",
"url": "https://files.pythonhosted.org/packages/cd/0c/b6e940d93ffd6b50bad262d20f7eaeaa1dc9f4e28a84574d52daed9fed5c/json_schema_tool-0.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "7b8065232d7dea33c876c17e46cb61ced5a1c3880d0f7cf160c4b2b94160f6f1",
"md5": "0f0a0caf7868f4dd70504d6c472db33a",
"sha256": "2b90109a44ae3fe6237e8da9ff313aff2eb48a4ad367ca1734e9860110f9ffb7"
},
"downloads": -1,
"filename": "json_schema_tool-0.4.0.tar.gz",
"has_sig": false,
"md5_digest": "0f0a0caf7868f4dd70504d6c472db33a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6",
"size": 18759,
"upload_time": "2024-07-19T10:31:24",
"upload_time_iso_8601": "2024-07-19T10:31:24.200879Z",
"url": "https://files.pythonhosted.org/packages/7b/80/65232d7dea33c876c17e46cb61ced5a1c3880d0f7cf160c4b2b94160f6f1/json_schema_tool-0.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-19 10:31:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ifak",
"github_project": "json_schema_tool",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "json-schema-tool"
}