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.
pip install jsonschema-specVerified import paths — ran on the pinned version, not inferred.
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.
Upgrade to `jsonschema-spec>=0.4.5`. Review the change log for `0.4.5` and `referencing` library updates, as this fix was introduced to restore compatibility. Adjust custom resolver implementations as per the latest `referencing` API.
Enable the cache by setting `resolved_cache_maxsize` when creating `SchemaPath` or `SchemaAccessor` instances. For example: `SchemaPath.from_dict(schema_dict, resolved_cache_maxsize=64)`.
Always specify the `$schema` keyword in your JSON Schemas to indicate the intended draft version (e.g., `"$schema": "https://json-schema.org/draft/2020-12/schema"`). Be aware of the specific changes between drafts (e.g., `$recursiveRef` to `$dynamicRef`, `items` array form) when working with schemas from different specifications.
Ensure the library is correctly installed using pip: `pip install jsonschema-spec`
Verify the exact property name in your JSON Schema. If the property may not exist, use dictionary-style access with a `.get()` method or check for its presence first: `schema.get('non_existent_property')` or `if 'non_existent_property' in schema: ...`Verify the exact key name in your JSON Schema. To safely access potentially missing keys, use the `.get()` method: `schema.get('non_existent_key', default_value)`Check the `$ref` URL or JSON Pointer path for correctness. Ensure all referenced schemas are accessible and correctly formatted. If referencing local files, verify the file paths and ensure they are loaded into the resolver if necessary.
from jsonschema_spec.accessors import SchemaAccessor