(
    meta_schema,
    validators=(),
    version=None,
    type_checker=_types.draft7_type_checker,
    id_of=_id_of,
    applicable_validators=lambda schema: schema.items(),
)
return Validator
Create a new validator class.

    Arguments:

        meta_schema (collections.abc.Mapping):

            the meta schema for the new validator class

        validators (collections.abc.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.

        id_of (collections.abc.Callable):

            A function that given a schema, returns its ID.

        applicable_validators (collections.abc.Callable):

            A function that given a schema, returns the list of applicable
            validators (names and callables) which will be called to
            validate the instance.

    Returns:

        a new `jsonschema.protocols.Validator` class
Create a new validator class.
Create a new validator class .
def create(
    meta_schema,
    validators=(),
    version=None,
    type_checker=_types.draft7_type_checker,
    id_of=_id_of,
    applicable_validators=lambda schema: schema.items(),
):
    """
    Create a new validator class.

    Arguments:

        meta_schema (collections.abc.Mapping):

            the meta schema for the new validator class

        validators (collections.abc.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.

        id_of (collections.abc.Callable):

            A function that given a schema, returns its ID.

        applicable_validators (collections.abc.Callable):

            A function that given a schema, returns the list of applicable
            validators (names and callables) which will be called to
            validate the instance.

    Returns:

        a new `jsonschema.protocols.Validator` class
    """

    @attr.s
    class Validator:

        VALIDATORS = dict(validators)
        META_SCHEMA = dict(meta_schema)
        TYPE_CHECKER = type_checker
        ID_OF = staticmethod(id_of)

        schema = attr.ib(repr=reprlib.repr)
        resolver = attr.ib(default=None, repr=False)
        format_checker = attr.ib(default=None)
        evolve = attr.evolve

        def __attrs_post_init__(self):
            if self.resolver is None:
                self.resolver = RefResolver.from_schema(
                    self.schema,
                    id_of=id_of,
                )

        @classmethod
        def check_schema(cls, schema):
            for error in cls(cls.META_SCHEMA).iter_errors(schema):
                raise exceptions.SchemaError.create_from(error)

        def iter_errors(self, instance, _schema=None):
            if _schema is not None:
                warnings.warn(
                    (
                        "Passing a schema to Validator.iter_errors "
                        "is deprecated and will be removed in a future "
                        "release. Call validator.evolve(schema=new_schema)."
                        "iter_errors(...) instead."
                    ),
                    DeprecationWarning,
                    stacklevel=2,
                )
            else:
                _schema = self.schema

            if _schema is True:
                return
            elif _schema is False:
                yield exceptions.ValidationError(
                    f"False schema does not allow {instance!r}",
                    validator=None,
                    validator_value=None,
                    instance=instance,
                    schema=_schema,
                )
                return

            scope = id_of(_schema)
            if scope:
                self.resolver.push_scope(scope)
            try:
                for k, v in applicable_validators(_schema):
                    validator = self.VALIDATORS.get(k)
                    if validator is None:
                        continue

                    errors = validator(self, v, instance, _schema) or ()
                    for error in errors:
                        # set details if not already set by the called fn
                        error._set(
                            validator=k,
                            validator_value=v,
                            instance=instance,
                            schema=_schema,
                        )
                        if k not in {"if", "$ref"}:
                            error.schema_path.appendleft(k)
                        yield error
            finally:
                if scope:
                    self.resolver.pop_scope()

        def descend(self, instance, schema, path=None, schema_path=None):
            for error in self.evolve(schema=schema).iter_errors(instance):
                if path is not None:
                    error.path.appendleft(path)
                if schema_path is not None:
                    error.schema_path.appendleft(schema_path)
                yield error

        def validate(self, *args, **kwargs):
            for error in self.iter_errors(*args, **kwargs):
                raise error

        def is_type(self, instance, type):
            try:
                return self.TYPE_CHECKER.is_type(instance, type)
            except exceptions.UndefinedTypeCheck:
                raise exceptions.UnknownType(type, instance, self.schema)

        def is_valid(self, instance, _schema=None):
            if _schema is not None:
                warnings.warn(
                    (
                        "Passing a schema to Validator.is_valid is deprecated "
                        "and will be removed in a future release. Call "
                        "validator.evolve(schema=new_schema).is_valid(...) "
                        "instead."
                    ),
                    DeprecationWarning,
                    stacklevel=2,
                )
                self = self.evolve(schema=_schema)

            error = next(self.iter_errors(instance), None)
            return error is None

    if version is not None:
        safe = version.title().replace(" ", "").replace("-", "")
        Validator.__name__ = Validator.__qualname__ = f"{safe}Validator"
        Validator = validates(version)(Validator)

    return Validator
def create ( meta_schema , validators = ( ) , version = None , type_checker = _types . draft7_type_checker , id_of = _id_of , applicable_validators = lambda schema : schema . items ( ) , ) : @ attr . s class Validator : VALIDATORS = dict ( validators ) META_SCHEMA = dict ( meta_schema ) TYPE_CHECKER = type_checker ID_OF = staticmethod ( id_of ) schema = attr . ib ( repr = reprlib . repr ) resolver = attr . ib ( default = None , repr = False ) format_checker = attr . ib ( default = None ) evolve = attr . evolve def __attrs_post_init__ ( self ) : if self . resolver is None : self . resolver = RefResolver . from_schema ( self . schema , id_of = id_of , ) @ classmethod def check_schema ( cls , schema ) : for error in cls ( cls . META_SCHEMA ) . iter_errors ( schema ) : raise exceptions . SchemaError . create_from ( error ) def iter_errors ( self , instance , _schema = None ) : if _schema is not None : warnings . warn ( ( "Passing a schema to Validator.iter_errors " "is deprecated and will be removed in a future " "release. Call validator.evolve(schema=new_schema)." "iter_errors(...) instead." ) , DeprecationWarning , stacklevel = 2 , ) else : _schema = self . schema if _schema is True : return elif _schema is False : yield exceptions . ValidationError ( f"False schema does not allow {instance!r}" , validator = None , validator_value = None , instance = instance , schema = _schema , ) return scope = id_of ( _schema ) if scope : self . resolver . push_scope ( scope ) try : for k , v in applicable_validators ( _schema ) : validator = self . VALIDATORS . get ( k ) if validator is None : continue errors = validator ( self , v , instance , _schema ) or ( ) for error in errors : # set details if not already set by the called fn error . _set ( validator = k , validator_value = v , instance = instance , schema = _schema , ) if k not in { "if" , "$ref" } : error . schema_path . appendleft ( k ) yield error finally : if scope : self . resolver . pop_scope ( ) def descend ( self , instance , schema , path = None , schema_path = None ) : for error in self . evolve ( schema = schema ) . iter_errors ( instance ) : if path is not None : error . path . appendleft ( path ) if schema_path is not None : error . schema_path . appendleft ( schema_path ) yield error def validate ( self , * args , * * kwargs ) : for error in self . iter_errors ( * args , * * kwargs ) : raise error def is_type ( self , instance , type ) : try : return self . TYPE_CHECKER . is_type ( instance , type ) except exceptions . UndefinedTypeCheck : raise exceptions . UnknownType ( type , instance , self . schema ) def is_valid ( self , instance , _schema = None ) : if _schema is not None : warnings . warn ( ( "Passing a schema to Validator.is_valid is deprecated " "and will be removed in a future release. Call " "validator.evolve(schema=new_schema).is_valid(...) " "instead." ) , DeprecationWarning , stacklevel = 2 , ) self = self . evolve ( schema = _schema ) error = next ( self . iter_errors ( instance ) , None ) return error is None if version is not None : safe = version . title ( ) . replace ( " " , "" ) . replace ( "-" , "" ) Validator . __name__ = Validator . __qualname__ = f"{safe}Validator" Validator = validates ( version ) ( Validator ) return Validator