jschon.jsonschema

class jschon.jsonschema.JSONSchema(value, *, catalog='catalog', cacheid='default', uri=None, metaschema_uri=None, parent=None, key=None)

JSON schema document model.

__init__(value, *, catalog='catalog', cacheid='default', uri=None, metaschema_uri=None, parent=None, key=None)

Initialize a JSONSchema instance from the given schema-compatible value.

Parameters
  • value (Union[bool, Mapping[str, JSONCompatible]]) – a schema-compatible Python object

  • catalog (Union[str, Catalog]) – catalog instance or catalog name

  • cacheid (Hashable) – schema cache identifier

  • uri (URI) – the URI identifying the schema; an "$id" keyword appearing in value will override this

  • metaschema_uri (URI) – the URI identifying the schema’s metaschema; a "$schema" keyword appearing in value will override this

  • parent (JSON) – the parent node of the schema; used internally when creating a subschema

  • key (str) – the index of the schema within its parent; used internally when creating a subschema

evaluate(instance, result=None)

Evaluate a JSON document and return the evaluation result.

Parameters
Return type

jschon.jsonschema.Result

validate()

Validate the schema against its metaschema.

Return type

jschon.jsonschema.Result

property base_uri: Optional[jschon.uri.URI]

The schema’s base URI.

The base URI is obtained by searching up the schema tree for a schema URI, and removing any fragment.

cacheid: Hashable

Schema cache identifier.

property canonical_uri: Optional[jschon.uri.URI]

The absolute location of the (sub)schema.

This is not necessarily an ‘absolute URI’, as it may contain a fragment.

catalog: Catalog

The catalog in which the schema is cached.

data: Union[None, bool, int, float, str, List[JSON], Dict[str, JSON]]

The schema data.

JSON type

data type

boolean

bool

object

dict[str, JSON]

itemclass: Type[JSON]

The JSON class type of child instances.

itemkwargs: Dict[str, Any]

Keyword arguments to the itemclass constructor.

key: Optional[str]

The index of the schema within its parent.

keywords: Dict[str, Keyword]

A dictionary of the schema’s Keyword objects, indexed by keyword name.

property metaschema: Metaschema

The schema’s Metaschema.

property metaschema_uri: Optional[jschon.uri.URI]

The URI identifying the schema’s metaschema.

If not defined on this (sub)schema, the metaschema URI is determined by the parent schema.

parent: Optional[JSON]

The containing JSON or JSONSchema node.

property parentschema: Optional[jschon.jsonschema.JSONSchema]

The containing JSONSchema instance.

Note that this is not necessarily the same as self.parent.

type: str

The JSON type of the schema. One of "boolean", "object".

property uri: Optional[jschon.uri.URI]

The URI identifying the schema.

Used as the key for caching the schema in the catalog.

class jschon.jsonschema.Result(schema, instance, *, parent=None, key=None)

The result of evaluating a JSON document node against a JSON schema node.

The root of a Result tree represents a complete document evaluation result.

Parameters
Return type

None

__call__(instance, key, schema=None, *, cls=None)

Yield a subresult for the evaluation of instance. Descend down the evaluation path by key, into schema if given, or within self.schema otherwise.

Extension keywords may provide a custom Result class via cls, which is applied to all nodes within the yielded subtree.

Parameters
Return type

AbstractContextManager[jschon.jsonschema.Result]

__init__(schema, instance, *, parent=None, key=None)
Parameters
Return type

None

annotate(value)

Annotate the result.

Parameters

value (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

None

collect_annotations(instance=None, key=None)

Return an iterator over annotations produced in this subtree, optionally filtered by instance and/or keyword.

Parameters
Return type

Iterator[Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]]

collect_errors(instance=None, key=None)

Return an iterator over errors produced in this subtree, optionally filtered by instance and/or keyword.

Parameters
Return type

Iterator[Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]]

discard()

Indicate that the result should be ignored and discarded.

Return type

None

fail(error=None)

Mark the result as invalid, optionally with an error.

Parameters

error (Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]) –

Return type

None

noassert()

Indicate that evaluation passes regardless of validity.

Return type

None

output(format, **kwargs)

Return the evaluation result in the specified format.

Parameters
  • format (str) – One of the standard JSON Schema output formats – flag, basic, detailed or verbose – or any format registered with the output_formatter() decorator.

  • kwargs (Any) – Keyword arguments to pass to the output formatter.

Return type

Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

pass_()

Mark the result as valid.

A result is initially valid, so this should only need to be called by a keyword when it must reverse a failure.

Return type

None

refschema(schema)

Set the referenced schema for a by-reference keyword.

This ensures that absolute_uri returns the URI of the referenced schema rather than the referencing keyword.

Parameters

schema (jschon.jsonschema.JSONSchema) –

Return type

None

sibling(instance, key)

Return a sibling schema node’s evaluation result for instance.

Parameters
Return type

Optional[jschon.jsonschema.Result]

property absolute_uri: Optional[jschon.uri.URI]

Return the absolute URI of the current schema node.

annotation: Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

The annotation value of the result.

children: Dict[Tuple[str, jschon.jsonpointer.JSONPointer], jschon.jsonschema.Result]

Subresults of the current result node, indexed by schema key and instance path.

error: Union[None, bool, int, float, str, Sequence[Any], Mapping[str, Any]]

The error value of the result.

property globals: Dict
instance: jschon.json.JSON

The instance under evaluation.

key: Optional[str]

The index of the current schema node within its dynamic parent.

parent: Optional[jschon.jsonschema.Result]

The parent result node.

property passed: bool

Return the assertion result for the schema node.

In the standard JSON Schema vocabulary, this can only differ from valid for the if keyword: validity may be false (triggering else) while its assertion result is always true.

For the root result node, passed will always equal valid.

path: jschon.jsonpointer.JSONPointer

The dynamic evaluation path to the current schema node.

relpath: jschon.jsonpointer.JSONPointer

The path to the current schema node relative to the evaluating (sub)schema.

schema: jschon.jsonschema.JSONSchema

The evaluating (sub)schema.

property schema_node: jschon.json.JSON

Return the current schema node.

property valid: bool

Return the validity of the instance against the schema.