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, value):
"""
Validator for decoded values.
:param value: The Python's object that has to be validated
"""
if not isinstance(value, self.python_type):
raise XMLSchemaValidationError(
self, value, "value type is {} instead of {}".format(type(value), repr(self.python_type))
)
if not all([validator(value) for validator in self.validators]):
raise XMLSchemaValidationError(self, value)
def validate(self, value):
"""
Validator for decoded values.
:param value: The Python's object that has to be validated
"""
if not isinstance(value, self.python_type):
raise XMLSchemaValidationError(
self, value, "value type is {} instead of {}".format(type(value), repr(self.python_type))
)
if not all([validator(value) for validator in self.validators]):
raise XMLSchemaValidationError(self, value)
def validate(self, value):
self.base_type.validate(value)
if not all([validator(value) for validator in self.validators]):
raise XMLSchemaValidationError(self, value)
if self.enumeration and value not in self.enumeration:
raise XMLSchemaValidationError(
self, value, reason="invalid value, it must be one of %r" % self.enumeration
)
try:
validation_group[_index] = (next(_iterator), _iterator, _container)
except StopIteration:
validation_group = []
break
if _container.model == XSD_CHOICE_TAG:
# With choice model reduce the validation_group
# in order to match only the first matched tag.
validation_group = [
(e, g, c) for e, g, c in validation_group if c != _container or g == _iterator
]
missing_tags.intersection_update({e[0].name for e in validation_group})
break
else:
if missing_tags:
yield XMLSchemaValidationError(self, child, "invalid tag", child, self.elem)
consumed_child = True
break
target_namespace = self.schema.target_namespace
# Verify instance attributes
for key, value in attributes.items():
qname = get_qname(target_namespace, key)
try:
xsd_attribute = self[qname]
required_attributes.discard(qname)
except KeyError:
qname, namespace = split_reference(key, self._namespaces)
if namespace == XSI_NAMESPACE_PATH:
lookup_attribute(qname, namespace, self._lookup_table).validate(value)
elif any_attribute is not None:
any_attribute.validate({qname: value})
else:
yield XMLSchemaValidationError(
self, key, "attribute not allowed for this element", elem, self.elem
)
else:
try:
xsd_attribute.decode(value)
except (XMLSchemaValidationError, XMLSchemaDecodeError) as err:
yield XMLSchemaValidationError(
xsd_attribute, err.value, err.reason, elem, xsd_attribute.elem
)
if required_attributes:
yield XMLSchemaValidationError(
self,
elem.attrib,
reason="missing required attributes %r" % required_attributes,
elem=elem,
)
# Validate child elements
model_generator = self.model_generator()
elem_iterator = iter(elem)
target_namespace = self.schema.target_namespace
consumed_child = True
while True:
try:
content_model = next(model_generator)
validation_group = []
for g, c in linked_flatten(content_model):
validation_group.extend([t for t in meta_next_gen(g, c)])
except StopIteration:
for child in elem_iterator:
yield XMLSchemaValidationError(self, child, "invalid tag", child, self.elem)
return
try:
missing_tags = set([e[0].name for e in validation_group if not e[0].is_optional()])
except (AttributeError, TypeError):
raise
while validation_group:
if consumed_child:
try:
child = next(elem_iterator)
except StopIteration:
if missing_tags:
yield XMLSchemaValidationError(
self, elem, "tag expected: %r" % tuple(missing_tags), elem, self.elem
)
for child in elem_iterator:
yield XMLSchemaValidationError(self, child, "invalid tag", child, self.elem)
return
try:
missing_tags = set([e[0].name for e in validation_group if not e[0].is_optional()])
except (AttributeError, TypeError):
raise
while validation_group:
if consumed_child:
try:
child = next(elem_iterator)
except StopIteration:
if missing_tags:
yield XMLSchemaValidationError(
self, elem, "tag expected: %r" % tuple(missing_tags), elem, self.elem
)
return
else:
name = get_qname(target_namespace, child.tag)
consumed_child = False
for _index, (_element, _iterator, _container) in enumerate(validation_group):
if name == _element.name:
consumed_child = True
missing_tags.discard(name)
try:
validation_group[_index] = (next(_iterator), _iterator, _container)
except StopIteration:
validation_group = []
break
elif any_attribute is not None:
any_attribute.validate({qname: value})
else:
yield XMLSchemaValidationError(
self, key, "attribute not allowed for this element", elem, self.elem
)
else:
try:
xsd_attribute.decode(value)
except (XMLSchemaValidationError, XMLSchemaDecodeError) as err:
yield XMLSchemaValidationError(
xsd_attribute, err.value, err.reason, elem, xsd_attribute.elem
)
if required_attributes:
yield XMLSchemaValidationError(
self,
elem.attrib,
reason="missing required attributes %r" % required_attributes,
elem=elem,
schema_elem=self.elem
)