OpenQuizz
Une application de gestion des contenus pédagogiques
jsonschema.validators Namespace Reference

Data Structures

class  _DefaultTypesDeprecatingMetaClass
 
class  _DontDoThat
 
class  RefResolver
 

Functions

def validates (version)
 
def create (meta_schema, validators=(), version=None, default_types=None, type_checker=None, id_of=_id_of)
 
def extend (validator, validators=(), version=None, type_checker=None)
 
def validate (instance, schema, cls=None, *args, **kwargs)
 
def validator_for (schema, default=_LATEST_VERSION)
 

Variables

 validators
 
 meta_schemas
 
 type_checkers
 
 Draft3Validator
 
 meta_schema
 
 type_checker
 
 version
 
 id_of
 
 Draft4Validator
 
 Draft6Validator
 
 Draft7Validator
 
 TYPE_CHECKER
 
 resolver
 
 format_checker
 
 schema
 

Detailed Description

Creation and extension of validators, with implementations for existing drafts.

Function Documentation

◆ create()

def jsonschema.validators.create (   meta_schema,
  validators = (),
  version = None,
  default_types = None,
  type_checker = None,
  id_of = _id_of 
)
Create a new validator class.

Arguments:

    meta_schema (collections.Mapping):

        the meta schema for the new validator class

    validators (collections.Mapping):

        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 `jsonschema.validators.validates` automatically
        called for the given version.

    type_checker (jsonschema.TypeChecker):

        a type checker, used when applying the :validator:`type` validator.

        If unprovided, a `jsonschema.TypeChecker` will be created
        with a set of default types typical of JSON Schema drafts.

    default_types (collections.Mapping):

        .. deprecated:: 3.0.0

            Please use the type_checker argument instead.

        If set, it provides mappings of JSON types to Python types
        that will be converted to functions and redefined in this
        object's `jsonschema.TypeChecker`.

    id_of (collections.Callable):

        A function that given a schema, returns its ID.

Returns:

    a new `jsonschema.IValidator` class

◆ extend()

def jsonschema.validators.extend (   validator,
  validators = (),
  version = None,
  type_checker = None 
)
Create a new validator class by extending an existing one.

Arguments:

    validator (jsonschema.IValidator):

        an existing validator class

    validators (collections.Mapping):

        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

    type_checker (jsonschema.TypeChecker):

        a type checker, used when applying the :validator:`type` validator.

        If unprovided, the type checker of the extended
        `jsonschema.IValidator` will be carried along.`

Returns:

    a new `jsonschema.IValidator` class extending the one provided

.. 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.

◆ validate()

def jsonschema.validators.validate (   instance,
  schema,
  cls = None,
args,
**  kwargs 
)
Validate an instance under the given schema.

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

:func:`validate` will first verify that the provided schema is
itself valid, since not doing so can lead to less obvious error
messages and fail in less obvious or consistent ways.

If you know you have a valid schema already, especially if you
intend to validate multiple instances with the same schema, you
likely would prefer using the `IValidator.validate` method directly
on a specific validator (e.g. ``Draft7Validator.validate``).


Arguments:

    instance:

        The instance to validate

    schema:

        The schema to validate with

    cls (IValidator):

        The class that will be used to validate the instance.

If the ``cls`` argument is not provided, two things will happen
in accordance with the specification. First, if the schema has a
:validator:`$schema` property containing a known meta-schema [#]_
then the proper validator will be used. The specification recommends
that all schemas contain :validator:`$schema` properties for this
reason. If no :validator:`$schema` property is found, the default
validator class is the latest released draft.

Any other provided positional and keyword arguments will be passed
on when instantiating the ``cls``.

Raises:

    `jsonschema.exceptions.ValidationError` if the instance
        is invalid

    `jsonschema.exceptions.SchemaError` if the schema itself
        is invalid

.. rubric:: Footnotes
.. [#] known by a validator registered with
    `jsonschema.validators.validates`

◆ validates()

def jsonschema.validators.validates (   version)
Register the decorated validator for a ``version`` of the specification.

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

Arguments:

    version (str):

        An identifier to use as the version's name

Returns:

    collections.Callable:

        a class decorator to decorate the validator with the version

◆ validator_for()

def jsonschema.validators.validator_for (   schema,
  default = _LATEST_VERSION 
)
Retrieve the validator class appropriate for validating the given schema.

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

Arguments:

    schema (collections.Mapping or bool):

        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 the latest supported
        draft.

Variable Documentation

◆ Draft3Validator

Draft3Validator

◆ Draft4Validator

Draft4Validator

◆ Draft6Validator

Draft6Validator

◆ Draft7Validator

Draft7Validator

◆ format_checker

format_checker

◆ id_of

id_of

◆ meta_schema

meta_schema

◆ meta_schemas

meta_schemas

◆ resolver

resolver

◆ schema

schema

◆ TYPE_CHECKER

TYPE_CHECKER

◆ type_checker

type_checker

◆ type_checkers

type_checkers

◆ validators

validators

◆ version

version