Skip to content

Commit

Permalink
Overhauls the mechanics of schema validation
Browse files Browse the repository at this point in the history
Yippie, that makes the code more lightweight and much easier to
understand.
  • Loading branch information
funkyfuture committed Aug 4, 2018
1 parent 89270f0 commit b79cff7
Show file tree
Hide file tree
Showing 10 changed files with 1,887 additions and 1,922 deletions.
54 changes: 51 additions & 3 deletions cerberus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,68 @@
"""

from typing import Dict, Optional, Tuple, Union

import pkg_resources

from cerberus.validator import DocumentError, Validator
from cerberus.schema import rules_set_registry, schema_registry, SchemaError
from cerberus.utils import TypeDefinition
from cerberus.base import (
rules_set_registry,
schema_registry,
DocumentError,
TypeDefinition,
UnconcernedValidator,
)
from cerberus.validator import Validator
from cerberus.schema import SchemaError


__version__ = pkg_resources.get_distribution('Cerberus').version


def validator_factory(
name: str,
bases: Union[type, Tuple[type], None] = None,
namespace: Optional[Dict] = None,
validated_schema: bool = True,
) -> type:
""" Dynamically create a :class:`~cerberus.Validator` subclass.
Docstrings of mixin-classes will be added to the resulting
class' one if ``__doc__`` is not in :obj:`namespace`.
:param name: The name of the new class.
:param bases: Class(es) with additional and overriding attributes.
:param namespace: Attributes for the new class.
:param validated_schema: Indicates that schemas that are provided to the validator
are to be validated.
:return: The created class.
"""

validator_class = Validator if validated_schema else UnconcernedValidator

if namespace is None:
namespace = {}

if bases is None:
computed_bases = (validator_class,)
elif isinstance(bases, tuple) and validator_class not in bases:
computed_bases = bases + (validator_class,) # type: ignore
else:
computed_bases = (bases, validator_class) # type: ignore

docstrings = [x.__doc__ for x in computed_bases if x.__doc__]
if len(docstrings) > 1 and '__doc__' not in namespace:
namespace.update({'__doc__': '\n'.join(docstrings)})

return type(name, computed_bases, namespace)


__all__ = [
DocumentError.__name__,
SchemaError.__name__,
TypeDefinition.__name__,
UnconcernedValidator.__name__,
Validator.__name__,
'schema_registry',
'rules_set_registry',
validator_factory.__name__,
]
Loading

0 comments on commit b79cff7

Please sign in to comment.