JSONSchema Spec

0.4.5 · active · verified Sat Apr 11

jsonschema-spec is a Python library (current version 0.4.5) that provides object-oriented paths for traversing and accessing JSON Schemas. It allows developers to interact with schema elements and their references in a programmatic way, abstracting away raw dictionary manipulation. The library sees active development, with frequent patch releases and minor updates to enhance features and ensure compatibility with related tools.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `SchemaPath` from a dictionary representing a JSON Schema and then traverse it using path-like syntax. It shows accessing properties, definitions, and performing implicit dereferencing.

from jsonschema_spec import SchemaPath

# Define a simple JSON Schema
schema_dict = {
    "type": "object",
    "properties": {
        "name": {"type": "string"},
        "age": {"type": "integer", "minimum": 0}
    },
    "$defs": {
        "Person": {
            "type": "object",
            "properties": {
                "firstName": {"type": "string"},
                "lastName": {"type": "string"}
            }
        }
    },
    "$ref": "#/$defs/Person"
}

# Create a SchemaPath object from the dictionary
path = SchemaPath.from_dict(schema_dict)

# Traverse the schema using path-like access
name_prop_path = path / "properties" / "name"
print(f"Name property schema: {name_prop_path.get_value()}")
# Expected: {'type': 'string'}

# Access a definition using implicit dereferencing
person_def_path = path / '$defs' / 'Person'
print(f"Person definition schema: {person_def_path.get_value()}")
# Expected: {'type': 'object', 'properties': {'firstName': {'type': 'string'}, 'lastName': {'type': 'string'}}}

# Directly dereference the root '$ref'
dereferenced_root = path.dereference()
print(f"Dereferenced root schema type: {dereferenced_root.get_value()['type']}")
# Expected: object

# Check for keys
print(f"'properties' in root path: {'properties' in path}")
# Expected: True

view raw JSON →