Skip to main content

A pythonic, extensible JSON Schema implementation.

Project description

jschon

Test Status Code Coverage Python Versions PyPI Documentation Status

jschon is a pythonic and extensible implementation of the JSON Schema specification.

Features

  • JSON Schema 2019-09 and 2020-12 vocabulary implementations
  • Support for custom metaschemas, vocabularies and format validators
  • JSON class implementing the JSON data model
  • JSON pointer implementation (RFC 6901)

Installation

pip install jschon

Usage

A simple example illustrates several key aspects of jschon usage:

  • Catalog setup
  • Construction of a compiled, reusable JSONSchema object
  • Validation of the schema by a metaschema
  • Construction of a JSON instance
  • Evaluation of the instance
  • Output generation
import pprint

from jschon import create_catalog, JSON, JSONSchema

# initialize the catalog, with JSON Schema 2020-12 vocabulary support
create_catalog('2020-12', default=True)

# create a schema to validate a JSON greeting object
schema = JSONSchema({
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "https://example.com/greeting",
    "type": "object",
    "properties": {
        "greeting": {"$ref": "#/$defs/greetingDefinition"}
    },
    "$defs": {
        "greetingDefinition": {
            "type": "string",
            "pattern": "^Hello, .+!$"
        }
    }
})

# validate the schema against its metaschema
schema_validity = schema.validate()
print(f'Schema validity check: {schema_validity.valid}')

# declare a valid JSON instance
valid_json = JSON({
    "greeting": "Hello, World!"
})

# declare an invalid JSON instance
invalid_json = JSON({
    "greeting": "Hi, World."
})

# evaluate the valid instance
valid_result = schema.evaluate(valid_json)

# evaluate the invalid instance
invalid_result = schema.evaluate(invalid_json)

# print output for the valid case
print(f'Valid JSON result: {valid_result.valid}')
print('Valid JSON basic output:')
pprint.pp(valid_result.output('basic'))

# print output for the invalid case
print(f'Invalid JSON result: {invalid_result.valid}')
print('Invalid JSON detailed output:')
pprint.pp(invalid_result.output('detailed'))

The script produces the following output:

Schema validity check: True
Valid JSON result: True
Valid JSON basic output:
{'valid': True,
 'annotations': [{'instanceLocation': '',
                  'keywordLocation': '/properties',
                  'absoluteKeywordLocation': 'https://example.com/greeting#/properties',
                  'annotation': ['greeting']}]}
Invalid JSON result: False
Invalid JSON detailed output:
{'valid': False,
 'instanceLocation': '',
 'keywordLocation': '',
 'absoluteKeywordLocation': 'https://example.com/greeting#',
 'errors': [{'instanceLocation': '/greeting',
             'keywordLocation': '/properties/greeting/$ref/pattern',
             'absoluteKeywordLocation': 'https://example.com/greeting#/$defs/greetingDefinition/pattern',
             'error': 'The text must match the regular expression "^Hello, .+!$"'}]}

Documentation

Further examples of usage, and an API reference, are available at Read the Docs.

Additional resources

Running the tests

jschon is tested using the JSON Schema Test Suite (excluding optional and format tests), along with custom unit tests that make use of the Hypothesis testing library.

To run the tests, install jschon in editable mode, including testing dependencies:

pip install -e git+https://github.com/marksparkza/jschon.git#egg=jschon[test]

Then, cd to the jschon source directory (pip show jschon will give you the location), and type tox.

Note that a complete test run requires all of the supported Python versions (3.8, 3.9, 3.10) to be installed on your system.

Contributing

Please see the guidelines for contributing.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jschon-0.7.2.tar.gz (35.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

jschon-0.7.2-py3-none-any.whl (49.9 kB view details)

Uploaded Python 3

File details

Details for the file jschon-0.7.2.tar.gz.

File metadata

  • Download URL: jschon-0.7.2.tar.gz
  • Upload date:
  • Size: 35.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for jschon-0.7.2.tar.gz
Algorithm Hash digest
SHA256 45b0d690994f468cd2584606d5310936771862b3c3e07663c3a74f50f187da07
MD5 880a4575fd07d1058c8b1db66a314759
BLAKE2b-256 67d683245778e911d5cbb0caa42d770965e8db4e88aa0af3256e8dc140a4012c

See more details on using hashes here.

File details

Details for the file jschon-0.7.2-py3-none-any.whl.

File metadata

  • Download URL: jschon-0.7.2-py3-none-any.whl
  • Upload date:
  • Size: 49.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.7.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for jschon-0.7.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1e9d64158bb93d7c56e9a01a6eabcd8ea4a7c1046bf3b052f345958538b7c38d
MD5 feeeb63490883e141b0339c87b2adbc9
BLAKE2b-256 40c59d0033d1e7d31e5c5e82a354cf4127b0a40598554b493ed4de142d56a347

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page