Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _validate_static_map_list(self, validator, data, path, strict):
if util.is_map(validator) and not util.is_map(data):
return ["%s : '%s' is not a map" % (path, data)]
if util.is_list(validator) and not util.is_list(data):
return ["%s : '%s' is not a list" % (path, data)]
errors = []
if strict:
data_keys = set(util.get_keys(data))
validator_keys = set(util.get_keys(validator))
for key in data_keys - validator_keys:
error_path = path + DataPath(key)
errors += ['%s: Unexpected element' % error_path]
for key, sub_validator in util.get_iter(validator):
errors += self._validate_item(sub_validator,
data,
path,
strict,
key)
return errors
def validate(self, data, data_name, strict):
path = DataPath()
errors = self._validate(self._schema, data, path, strict)
if errors:
header = '\nError validating data %s with schema %s' % (data_name,
self.name)
error_str = header + '\n\t' + '\n\t'.join(errors)
if PY2:
error_str = error_str.encode('utf-8')
raise ValueError(error_str)
def __init__(self, schema_dict, name='', validators=None, includes=None):
self.validators = validators or val.DefaultValidators
self.dict = schema_dict
self.name = name
self._schema = self._process_schema(DataPath(),
schema_dict,
self.validators)
# if this schema is included it shares the includes with the top level
# schema
self.includes = {} if includes is None else includes
def _validate_item(self, validator, data, path, strict, key):
"""
Fetch item from data at the postion key and validate with validator.
Returns an array of errors.
"""
errors = []
path = path + DataPath(key)
try: # Pull value out of data. Data can be a map or a list/sequence
data_item = data[key]
except (KeyError, IndexError): # Oops, that field didn't exist.
# Optional? Who cares.
if isinstance(validator, val.Validator) and validator.is_optional:
return errors
# SHUT DOWN EVERTYHING
errors.append('%s: Required field missing' % path)
return errors
return self._validate(validator, data_item, path, strict)
def _process_schema(self, path, schema_data, validators):
"""
Go through a schema and construct validators.
"""
if util.is_map(schema_data) or util.is_list(schema_data):
for key, data in util.get_iter(schema_data):
schema_data[key] = self._process_schema(path + DataPath(key),
data,
validators)
else:
schema_data = self._parse_schema_item(path,
schema_data,
validators)
return schema_data