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(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)
]
while fields_with_default_setter:
field = fields_with_default_setter.pop(0)
try:
self._normalize_default_setter(mapping, schema, field)
except KeyError:
fields_with_default_setter.append(field)
except Exception as e:
self._error(field, errors.SETTING_DEFAULT_FAILED, str(e))
fields_processing_state = hash(tuple(fields_with_default_setter))
if fields_processing_state in known_fields_states:
for field in fields_with_default_setter:
self._error(
field,
errors.SETTING_DEFAULT_FAILED,
'Circular dependencies of default setters.',
)
break
else:
known_fields_states.add(fields_processing_state)
schema = dict(((i, schema) for i in range(len(value))))
validator = self._get_child_validator(
document_crumb=field,
schema_crumb=(field, 'schema'),
schema=schema,
allow_unknown=self.allow_unknown,
)
validator(
dict(((i, v) for i, v in enumerate(value))),
update=self.update,
normalize=False,
)
if validator._errors:
self._drop_nodes_from_errorpaths(validator._errors, [], [2])
self._error(field, errors.SEQUENCE_SCHEMA, validator._errors)
def __validate_schema_mapping(self, field, schema, value):
schema = self._resolve_schema(schema)
allow_unknown = self.schema[field].get('allow_unknown', self.allow_unknown)
require_all = self.schema[field].get('require_all', self.require_all)
validator = self._get_child_validator(
document_crumb=field,
schema_crumb=(field, 'schema'),
schema=schema,
allow_unknown=allow_unknown,
require_all=require_all,
)
try:
if not validator(value, update=self.update, normalize=False):
self._error(field, errors.MAPPING_SCHEMA, validator._errors)
except _SchemaRuleTypeError:
self._error(field, errors.BAD_TYPE_FOR_SCHEMA)
raise
def __validate_logical(self, operator, definitions, field, value):
""" Validates value against all definitions and logs errors according
to the operator. """
valid_counter = 0
_errors = errors.ErrorList()
for i, definition in enumerate(definitions):
schema = {field: definition.copy()}
for rule in ('allow_unknown', 'type'):
if rule not in schema[field] and rule in self.schema[field]:
schema[field][rule] = self.schema[field][rule]
if 'allow_unknown' not in schema[field]:
schema[field]['allow_unknown'] = self.allow_unknown
validator = self._get_child_validator(
schema_crumb=(field, operator, i), schema=schema, allow_unknown=True
)
if validator(self.document, update=self.update, normalize=False):
valid_counter += 1
else:
self._drop_nodes_from_errorpaths(validator._errors, [], [3])
validated_molecules = molecules_description.copy()
# Schema validation
for molecule_id, molecule_descr in molecules_description.items():
small_molecule_validator = schema.YANKCerberusValidator(small_molecule_schema)
peptide_validator = schema.YANKCerberusValidator(peptide_schema)
# Test for small molecule
if small_molecule_validator.validate(molecule_descr):
validated_molecules[molecule_id] = small_molecule_validator.document
# Test for peptide
elif peptide_validator.validate(molecule_descr):
validated_molecules[molecule_id] = peptide_validator.document
else:
# Both failed, lets figure out why
# Check the is peptide w/ only excluded errors
if (cerberus.errors.EXCLUDES_FIELD in peptide_validator._errors and
peptide_validator.document_error_tree['filepath'] is None):
error = ("Molecule {} appears to be a peptide, but uses items exclusive to small molecules:\n"
"Please change either the options to peptide-only entries, or your molecule to a "
"small molecule.\n"
"====== Peptide Schema =====\n"
"{}\n"
"===========================\n")
error = error.format(molecule_id, yaml.dump(peptide_validator.errors))
# We don't know exactly what went wrong, run both error blocks
else:
error = ("Molecule {} failed to validate against one of the following schemes\n"
"Please check the following schemes for errors:\n"
"===========================\n"
"== Small Molecule Schema ==\n"
"{}\n"
"===========================\n\n" # Blank line
def _validate_logical(self, rule, field, value):
""" {'allowed': ('allof', 'anyof', 'noneof', 'oneof')} """
if not isinstance(value, Sequence):
self._error(field, errors.BAD_TYPE)
return
validator = self._get_child_validator(
document_crumb=rule,
allow_unknown=False,
schema=self.target_validator.validation_rules,
)
for constraints in value:
_hash = (
mapping_hash({'turing': constraints}),
mapping_hash(self.target_validator.types_mapping),
)
if _hash in self.target_validator._valid_schemas:
continue