Creating or Extending Validator Classes

jsonschema.validators.create(meta_schema, validators=(), version=None, default_types=None)[source]

Create a new validator class.

Parameters:
  • meta_schema (dict) – the meta schema for the new validator class
  • validators (dict) –

    a mapping from names to callables, where each callable will validate the schema property with the given name.

    Each callable should take 4 arguments:

    1. a validator instance,
    2. the value of the property being validated within the instance
    3. the instance
    4. the schema
  • version (str) – an identifier for the version that this validator class will validate. If provided, the returned validator class will have its __name__ set to include the version, and also will have validates() automatically called for the given version.
  • default_types (dict) – a default mapping to use for instances of the validator class when mapping between JSON types to Python types. The default for this argument is probably fine. Instances can still have their types customized on a per-instance basis.
Returns:

a new jsonschema.IValidator class

jsonschema.validators.extend(validator, validators, version=None)[source]

Create a new validator class by extending an existing one.

Parameters:
  • validator (jsonschema.IValidator) – an existing validator class
  • validators (dict) –

    a mapping of new validator callables to extend with, whose structure is as in create().

    Note

    Any validator callables with the same name as an existing one will (silently) replace the old validator callable entirely, effectively overriding any validation done in the “parent” validator class.

    If you wish to instead extend the behavior of a parent’s validator callable, delegate and call it directly in the new validator function by retrieving it using OldValidator.VALIDATORS["validator_name"].

  • version (str) – a version for the new validator class
Returns:

a new jsonschema.IValidator class

Note

Meta Schemas

The new validator class will have its parent’s meta schema.

If you wish to change or extend the meta schema in the new validator class, modify META_SCHEMA directly on the returned class. Note that no implicit copying is done, so a copy should likely be made before modifying it, in order to not affect the old validator.

jsonschema.validators.validator_for(schema, default=<unset>)[source]

Retrieve the validator class appropriate for validating the given schema.

Uses the $schema property that should be present in the given schema to look up the appropriate validator class.

Parameters:
  • schema – the schema to look at
  • default – the default to return if the appropriate validator class cannot be determined. If unprovided, the default is to return Draft4Validator
jsonschema.validators.validates(version)[source]

Register the decorated validator for a version of the specification.

Registered validators and their meta schemas will be considered when parsing $schema properties’ URIs.

Parameters:version (str) – An identifier to use as the version’s name
Returns:a class decorator to decorate the validator with the version
Return type:callable

Creating Validation Errors

Any validating function that validates against a subschema should call ValidatorMixin.descend(), rather than ValidatorMixin.iter_errors(). If it recurses into the instance, or schema, it should pass one or both of the path or schema_path arguments to ValidatorMixin.descend() in order to properly maintain where in the instance or schema respectively the error occurred.