Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Full documentation is available at http://python-cerberus.org/
"""
from __future__ import absolute_import
from cerberus.validator import DocumentError, Validator
from cerberus.schema import rules_set_registry, schema_registry, SchemaError
from cerberus.utils import TypeDefinition
__version__ = "1.2"
__all__ = [
DocumentError.__name__,
SchemaError.__name__,
TypeDefinition.__name__,
Validator.__name__,
'schema_registry',
'rules_set_registry',
]
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)
def __delitem__(self, key):
_new_schema = self.schema.copy()
try:
del _new_schema[key]
except ValueError:
raise SchemaError("Schema has no field '%s' defined" % key)
except Exception as e:
raise e
else:
del self.schema[key]
: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)
def __init_processing(self, document, schema=None):
self._errors = errors.ErrorList()
self.recent_error = None
self.document_error_tree = errors.DocumentErrorTree()
self.schema_error_tree = errors.SchemaErrorTree()
self.document = copy(document)
if not self.is_child:
self._is_normalized = False
if schema is not None:
self.schema = DefinitionSchema(self, schema)
elif self.schema is None:
if isinstance(self.allow_unknown, Mapping):
self._schema = {}
else:
raise SchemaError(errors.SCHEMA_ERROR_MISSING)
if document is None:
raise DocumentError(errors.DOCUMENT_MISSING)
if not isinstance(document, Mapping):
raise DocumentError(errors.DOCUMENT_FORMAT.format(document))
self.error_handler.start(self)
def update(self, schema):
try:
schema = self.expand(schema)
_new_schema = self.schema.copy()
_new_schema.update(schema)
self.validate(_new_schema)
except ValueError:
raise SchemaError(errors.SCHEMA_ERROR_DEFINITION_TYPE.format(schema))
except Exception as e:
raise e
else:
self.schema = _new_schema
def validate(schema_name, values):
try:
v = Validator(schemas.get(schema_name))
response = v.validate(values)
if response == False:
raise ValidationFailed(v.errors)
return True
except SchemaError:
raise ValidationSchemaMissing(schema_name)