Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
- the error-reference, see :mod:`cerberus.errors`
- arbitrary, supplemental information about the error
A :class:`~cerberus.errors.ValidationError` instance will
be created and added to
:attr:`~cerberus.Validator._errors`.
"""
if len(args) == 1:
self._errors.extend(args[0])
self._errors.sort()
for error in args[0]:
self.document_error_tree.add(error)
self.schema_error_tree.add(error)
self.error_handler.emit(error)
elif len(args) == 2 and isinstance(args[1], _str_type):
self._error(args[0], errors.CUSTOM, args[1])
elif len(args) >= 2:
field = args[0]
code = args[1].code
rule = args[1].rule
info = args[2:]
document_path = self.document_path + (field,)
schema_path = self.schema_path
if code != errors.UNKNOWN_FIELD.code and rule is not None:
schema_path += (field, rule)
if not rule:
constraint = None
else:
:type error_handler: class or instance based on
:class:`~cerberus.errors.BaseErrorHandler` or
:class:`tuple`
""" # noqa: E501
mandatory_validations = ('nullable',)
""" Rules that are evaluated on any field, regardless whether defined in
the schema or not.
Type: :class:`tuple` """
priority_validations = ('nullable', 'readonly', 'type', 'empty')
""" Rules that will be processed in that order before any other.
Type: :class:`tuple` """
types_mapping = {
'binary': TypeDefinition('binary', (bytes, bytearray), ()),
'boolean': TypeDefinition('boolean', (bool,), ()),
'container': TypeDefinition('container', (Container,), (_str_type,)),
'date': TypeDefinition('date', (date,), ()),
'datetime': TypeDefinition('datetime', (datetime,), ()),
'dict': TypeDefinition('dict', (Mapping,), ()),
'float': TypeDefinition('float', (float, _int_types), ()),
'integer': TypeDefinition('integer', (_int_types,), ()),
'list': TypeDefinition('list', (Sequence,), (_str_type,)),
'number': TypeDefinition('number', (_int_types, float), (bool,)),
'set': TypeDefinition('set', (set,), ()),
'string': TypeDefinition('string', (_str_type), ()),
}
""" This mapping holds all available constraints for the type rule and
their assigned :class:`~cerberus.TypeDefinition`. """
_valid_schemas = set()
""" A :class:`set` of hashes derived from validation schemas that are
legit for a particular ``Validator`` class. """
def _handle_schema_reference_for_validator(self, field, value):
if not isinstance(value, _str_type):
return value
if value in self.known_schema_refs:
raise _Abort
self.known_schema_refs += (value,)
definition = self.target_validator.schema_registry.get(value)
if definition is None:
path = self.document_path + (field,)
self._error(path, 'Schema definition {} not found.'.format(value))
raise _Abort
return definition
def quote_string(value):
if isinstance(value, _str_type):
return '"%s"' % value
else:
return value
def _validate(self, schema):
if isinstance(schema, _str_type):
schema = self.validator.schema_registry.get(schema, schema)
if schema is None:
raise SchemaError(errors.SCHEMA_ERROR_MISSING)
schema = copy(schema)
for field in schema:
if isinstance(schema[field], _str_type):
schema[field] = rules_set_registry.get(schema[field], schema[field])
if not self.schema_validator(schema, normalize=False):
raise SchemaError(self.schema_validator.errors)
def _check_with_bulk_schema(self, field, value):
# resolve schema registry reference
if isinstance(value, _str_type):
if value in self.known_rules_set_refs:
return
else:
self.known_rules_set_refs.add(value)
definition = self.target_validator.rules_set_registry.get(value)
if definition is None:
self._error(field, 'Rules set definition %s not found.' % value)
return
else:
value = definition
_hash = (
mapping_hash({'turing': value}),
mapping_hash(self.target_validator.types_mapping),
)
if _hash in self.target_validator._valid_schemas:
def _validate_allowed(self, allowed_values, field, value):
""" {'type': 'container'} """
if isinstance(value, Iterable) and not isinstance(value, _str_type):
unallowed = set(value) - set(allowed_values)
if unallowed:
self._error(field, errors.UNALLOWED_VALUES, list(unallowed))
else:
if value not in allowed_values:
self._error(field, errors.UNALLOWED_VALUE, value)
def compare_paths_lt(x, y):
min_length = min(len(x), len(y))
if x[:min_length] == y[:min_length]:
return len(x) == min_length
for i in range(min_length):
a, b = x[i], y[i]
for _type in (_int_types, _str_type, tuple):
if isinstance(a, _type):
if isinstance(b, _type):
break
else:
return True
if a == b:
continue
elif a < b:
return True
else:
return False
raise RuntimeError
:type error_handler: class or instance based on
:class:`~cerberus.errors.BaseErrorHandler` or
:class:`tuple`
""" # noqa: E501
mandatory_validations = ('nullable',)
""" Rules that are evaluated on any field, regardless whether defined in
the schema or not.
Type: :class:`tuple` """
priority_validations = ('nullable', 'readonly', 'type', 'empty')
""" Rules that will be processed in that order before any other.
Type: :class:`tuple` """
types_mapping = {
'binary': TypeDefinition('binary', (bytes, bytearray), ()),
'boolean': TypeDefinition('boolean', (bool,), ()),
'container': TypeDefinition('container', (Container,), (_str_type,)),
'date': TypeDefinition('date', (date,), ()),
'datetime': TypeDefinition('datetime', (datetime,), ()),
'dict': TypeDefinition('dict', (Mapping,), ()),
'float': TypeDefinition('float', (float, _int_types), ()),
'integer': TypeDefinition('integer', (_int_types,), ()),
'list': TypeDefinition('list', (Sequence,), (_str_type,)),
'number': TypeDefinition('number', (_int_types, float), (bool,)),
'set': TypeDefinition('set', (set,), ()),
'string': TypeDefinition('string', (_str_type), ()),
}
""" This mapping holds all available constraints for the type rule and
their assigned :class:`~cerberus.TypeDefinition`. """
_valid_schemas = set()
""" A :class:`set` of hashes derived from validation schemas that are
legit for a particular ``Validator`` class. """
def _validate(self, schema):
""" Validates a schema that defines rules against supported rules.
:param schema: The schema to be validated as a legal cerberus schema
according to the rules of this Validator object.
"""
if isinstance(schema, _str_type):
schema = self.validator.schema_registry.get(schema, schema)
if schema is None:
raise SchemaError(errors.SCHEMA_ERROR_MISSING)
schema = copy(schema)
for field in schema:
if isinstance(schema[field], _str_type):
schema[field] = rules_set_registry.get(schema[field], schema[field])
if not self.schema_validator(schema, normalize=False):
raise SchemaError(self.schema_validator.errors)