JSON Schema API Reference¶
Auto-generated API documentation for the jsonschema module.
jsonschema
¶
JSON Schema flattening, sanitization & validation — zero dependencies, stdlib only.
Flatten complex JSON Schemas for LLM providers, and validate data instances against JSON Schema (Draft 2020-12 subset used by OpenAPI 3.x).
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Flattening example::
>>> from jsonschema import flatten_schema
>>> schema = {
... "type": "object",
... "properties": {
... "user": {"$ref": "#/$defs/User"},
... },
... "$defs": {
... "User": {
... "type": "object",
... "properties": {"name": {"type": "string"}},
... }
... },
... }
>>> flatten_schema(schema)
{'type': 'object', 'properties': {'user': {'type': 'object', 'properties': {'name': {'type': 'string'}}}}}
Validation example::
>>> from jsonschema import schema_validate, iter_errors
>>> schema = {"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}
>>> schema_validate({"name": "Alice"}, schema)
>>> errors = iter_errors({}, schema)
>>> len(errors)
1
Supported validation keywords (Draft 2020-12 subset)::
type, enum, const,
minLength, maxLength, pattern,
minimum, maximum, exclusiveMinimum, exclusiveMaximum, multipleOf,
properties, required, additionalProperties, patternProperties,
minProperties, maxProperties,
items, prefixItems, minItems, maxItems, uniqueItems, contains,
allOf, anyOf, oneOf, not, if/then/else
Not implemented: dependentRequired, dependentSchemas, propertyNames, minContains, maxContains, format.
Note: OpenAPI 3.0 nullable: true is not handled by the validator.
Use flatten_schema to convert nullable to type: [..., "null"]
before validating, or write schemas using Draft 2020-12 type arrays.
Flattening pipeline::
resolve_refs → merge_allof → simplify_unions → sanitize
SchemaErrorDetail
dataclass
¶
A single JSON Schema validation error.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
str
|
Dotted/bracketed path to the failing instance location. |
schema_path |
str
|
Dotted path into the schema that triggered the error. |
validator |
str
|
The JSON Schema keyword that failed. |
message |
str
|
Human-readable error message. |
Source code in jsonschema/jsonschema.py
SchemaValidationError
¶
Bases: Exception
Raised when data validation against a JSON Schema fails.
Attributes:
| Name | Type | Description |
|---|---|---|
errors |
List of all validation errors found. |
Source code in jsonschema/jsonschema.py
resolve_refs(schema)
¶
Resolve all local $ref pointers and inline their targets.
Supports any JSON Pointer fragment (RFC 6901), including
#/$defs/, #/definitions/, #/components/schemas/, etc.
After resolution, $defs and definitions maps are removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with all |
Source code in jsonschema/jsonschema.py
merge_allof(schema)
¶
Deep-merge all allOf sub-schemas into single schemas.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict ( |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with all |
Source code in jsonschema/jsonschema.py
simplify_unions(schema)
¶
Simplify anyOf/oneOf constructs.
- Nullable pattern
[{type: T}, {type: null}]→{type: T, nullable: true} - Single-variant: unwrap.
- Multi-variant with null: strip the
{type: null}branch, keep remaining branches under the original keyword (anyOforoneOf).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with |
Source code in jsonschema/jsonschema.py
sanitize(schema, *, strip_keys=None)
¶
Strip unsupported schema keywords and validate required arrays.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict. |
required |
strip_keys
|
set[str] | None
|
Additional keys to strip beyond
:data: |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A new dict with unsupported keys removed and |
dict[str, Any]
|
pruned so that |
Source code in jsonschema/jsonschema.py
flatten_schema(schema, *, strip_keys=None)
¶
One-call full pipeline: resolve → merge → simplify → sanitize.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
schema
|
dict[str, Any]
|
A JSON Schema dict, possibly containing |
required |
strip_keys
|
set[str] | None
|
Additional keys to strip beyond
:data: |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A flattened, sanitized schema safe for LLM provider consumption. |
Source code in jsonschema/jsonschema.py
iter_errors(instance, schema, *, resolved=False)
¶
Validate instance against JSON Schema schema and return all errors.
The schema is preprocessed: $ref pointers are resolved before
validation. Composition keywords (allOf, anyOf, oneOf,
not) are evaluated semantically, not merged.
When validating many instances against the same schema, resolve once
and pass resolved=True to skip redundant $ref resolution::
schema = resolve_refs(raw_schema)
for item in items:
errors = iter_errors(item, schema, resolved=True)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
The data to validate. |
required |
schema
|
dict[str, Any] | bool
|
A JSON Schema dict, or a boolean schema. |
required |
resolved
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
list[SchemaErrorDetail]
|
A list of :class: |
Source code in jsonschema/jsonschema.py
schema_validate(instance, schema, *, resolved=False)
¶
Validate instance against JSON Schema schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instance
|
Any
|
The data to validate. |
required |
schema
|
dict[str, Any] | bool
|
A JSON Schema dict. |
required |
resolved
|
bool
|
If |
False
|
Raises:
| Type | Description |
|---|---|
SchemaValidationError
|
If validation fails, with all errors collected. |