jsonschema.protocols (original) (raw)

View this page

Toggle table of contents sidebar

typing.Protocol classes for jsonschema interfaces.

class jsonschema.protocols.Validator(schema: Mapping | bool, registry: referencing.jsonschema.SchemaRegistry, format_checker: jsonschema.FormatChecker | None = None)[source]

The protocol to which all validator classes adhere.

Parameters:

Deprecated since version v4.12.0: Subclassing validator classes now explicitly warns this is not part of their public API.

FORMAT_CHECKER_: ClassVar[jsonschema.FormatChecker]_

A jsonschema.FormatChecker that will be used when validatingformat keywords in JSON schemas.

ID_OF_: _typing.id_of_

A function which given a schema returns its ID.

META_SCHEMA_: ClassVar[Mapping]_

An object representing the validator’s meta schema (the schema that describes valid schemas in the given version).

TYPE_CHECKER_: ClassVar[jsonschema.TypeChecker]_

A jsonschema.TypeChecker that will be used when validatingtype keywords in JSON schemas.

VALIDATORS_: ClassVar[Mapping]_

A mapping of validation keywords (strs) to functions that validate the keyword with that name. For more information seeCreating or Extending Validator Classes.

classmethod check_schema(schema: Mapping | bool) → None[source]

Validate the given schema against the validator’s META_SCHEMA.

Raises:

jsonschema.exceptions.SchemaError – if the schema is invalid

evolve(**kwargs) → Validator[source]

Create a new validator like this one, but with given changes.

Preserves all other attributes, so can be used to e.g. create a validator with a different schema but with the same $refresolution behavior.

validator = Draft202012Validator({}) validator.evolve(schema={"type": "number"}) Draft202012Validator(schema={'type': 'number'}, format_checker=None)

The returned object satisfies the validator protocol, but may not be of the same concrete class! In particular this occurs when a $ref occurs to a schema with a different$schema than this one (i.e. for a different draft).

validator.evolve( ... schema={"$schema": Draft7Validator.META_SCHEMA["$id"]} ... ) Draft7Validator(schema=..., format_checker=None)

is_type(instance: Any, type: str) → bool[source]

Check if the instance is of the given (JSON Schema) type.

Parameters:

Returns:

whether the instance is of the given type

Raises:

jsonschema.exceptions.UnknownType – if type is not a known type

is_valid(instance: Any) → bool[source]

Check if the instance is valid under the current schema.

Returns:

whether the instance is valid or not

schema = {"maxItems" : 2} Draft202012Validator(schema).is_valid([2, 3, 4]) False

iter_errors(instance: Any) → Iterable[ValidationError][source]

Lazily yield each of the validation errors in the given instance.

schema = { ... "type" : "array", ... "items" : {"enum" : [1, 2, 3]}, ... "maxItems" : 2, ... } v = Draft202012Validator(schema) for error in sorted(v.iter_errors([2, 3, 4]), key=str): ... print(error.message) 4 is not one of [1, 2, 3] [2, 3, 4] is too long

Deprecated since version v4.0.0: Calling this function with a second schema argument is deprecated. Use Validator.evolve instead.

schema_: Mapping | bool_

The schema that will be used to validate instances

validate(instance: Any) → None[source]

Check if the instance is valid under the current schema.

Raises:

jsonschema.exceptions.ValidationError – if the instance is invalid

schema = {"maxItems" : 2} Draft202012Validator(schema).validate([2, 3, 4]) Traceback (most recent call last): ... ValidationError: [2, 3, 4] is too long