jschon.jsonschema

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

JSON schema document model.

__init__(value, *, catalog=None, session='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, AnyJSONCompatible]]) – a schema-compatible Python object

  • catalog (Catalog) – the catalog in which the schema is cached; omitting this parameter has the same effect as setting it to the default catalog, i.e. that created by jschon.create_catalog(…, default=True)

  • session (Hashable) – a session identifier, identifying which session cache to put the schema in

  • 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, scope=None)

Evaluate a JSON document.

The returned Scope represents the complete evaluation result tree for this (sub)schema node.

Parameters
Return type

jschon.jsonschema.Scope

validate()

Validate the schema against its metaschema.

Return type

jschon.jsonschema.Scope

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.

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, Decimal, str, List[JSON], Dict[str, JSON]]

The schema data.

JSON type

data type

boolean

bool

object

dict[str, JSON]

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.

session: Hashable

A session identifier, identifying which session cache to use for the schema.

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.OutputFormat(value)

An enumeration.

BASIC = 'basic'
DETAILED = 'detailed'
FLAG = 'flag'
VERBOSE = 'verbose'
class jschon.jsonschema.Scope(schema, *, key=None, path=None, relpath=None, instpath=None, parent=None)
Parameters
__call__(instance, key, schema=None)

Yield a subscope of the current scope, for evaluating instance. Descend down the evaluation path by key, into schema if given, or within the schema of the current scope otherwise.

Parameters
Return type

AbstractContextManager[jschon.jsonschema.Scope]

__init__(schema, *, key=None, path=None, relpath=None, instpath=None, parent=None)
Parameters
annotate(value)

Set an annotation on the scope.

Parameters

value (jschon.json.AnyJSONCompatible) –

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[jschon.json.AnyJSONCompatible]

discard()

Indicate that the scope should be ignored and discarded.

Return type

None

fail(error=None)

Flag the scope as invalid, optionally with an error message.

Parameters

error (Optional[str]) –

Return type

None

iter_children(instance=None)

Return an iterator over child scopes of this scope, optionally filtered by an instance to which they apply.

Parameters

instance (Optional[jschon.json.JSON]) –

Return type

Iterator[jschon.jsonschema.Scope]

noassert()

Indicate that the scope’s validity should not affect its assertion result.

Return type

None

output(format)

Return an output dictionary formatted in accordance with the JSON Schema specification of the given output format.

Parameters

format (jschon.jsonschema.OutputFormat) –

Return type

Dict[str, jschon.json.AnyJSONCompatible]

pass_()

Flag the scope as valid.

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

Return type

None

refschema(schema)

Set the referenced schema for the scope of a by-reference keyword.

The referenced schema’s URI is then returned by absolute_uri, instead of calculating the absolute URI using the containing schema.

Parameters

schema (jschon.jsonschema.JSONSchema) –

Return type

None

sibling(instance, key)
Parameters
Return type

Optional[jschon.jsonschema.Scope]

property absolute_uri: Optional[jschon.uri.URI]
property passed: bool

Return the assertion result of the scope.

In the current implementation, this can only ever differ from valid for an “if” keyword subscope: its validation result may be false (triggering “else”) while its assertion result is always true. For the root scope (representing the overall document evaluation result), valid will always equal passed.

Return type

bool

property valid: bool

Return the validation result of the scope.

Return type

bool