Metadata-Version: 2.1
Name: json_schema_tool
Version: 0.2.0
Summary: JSON Schema validation with various additional features
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.
        
Project-URL: Homepage, https://github.com/ifak/json_schema_tool
Keywords: python,json,json-schema
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.6
Description-Content-Type: text/markdown
License-File: LICENSE

# JSON Schema Tool

[![Tests](https://github.com/ifak/json_schema_tool/actions/workflows/check.yml/badge.svg)](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
```

## 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.
