How to use the xmlschema.validators.exceptions.XMLSchemaValidationError function in xmlschema

To help you get started, we’ve selected a few xmlschema examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sissaschool / xmlschema / xmlschema / validators / facets.py View on Github external
def base64_min_length_validator(self, x):
        x = x.replace(' ', '')
        if (len(x) // 4 * 3 - (x[-1] in ('=', 61)) - (x[-2] in ('=', 61))) < self.value:
            yield XMLSchemaValidationError(self, x, "binary length cannot be lesser than %r." % self.value)
github sissaschool / xmlschema / xmlschema / validators / groups.py View on Github external
xsd_type = xsd_element.type
            else:
                alternatives = xsd_element.alternatives
                try:
                    type_name = elem.attrib[XSI_TYPE].strip()
                except KeyError:
                    xsd_type = xsd_element.type
                else:
                    xsd_type = self.maps.get_instance_type(type_name, xsd_element.type, converter)

            if model_element is not xsd_element and model_element.block:
                for derivation in model_element.block.split():
                    if xsd_type is not model_element.type and \
                            xsd_type.is_derived(model_element.type, derivation):
                        reason = "usage of %r with type %s is blocked by head element"
                        raise XMLSchemaValidationError(self, elem, reason % (xsd_element, derivation))

            if XSI_TYPE not in elem.attrib:
                return

        # If it's a restriction the context is the base_type's group
        group = self.restriction if self.restriction is not None else self

        # Dynamic EDC check of matched element
        for e in filter(lambda x: isinstance(x, XsdElement), group.iter_elements()):
            if e.name == elem.tag:
                other = e
            else:
                for other in e.iter_substitutes():
                    if other.name == elem.tag:
                        break
                else:
github sissaschool / xmlschema / xmlschema / validators / facets.py View on Github external
def required_timezone_validator(self, x):
        if x.tzinfo is None:
            yield XMLSchemaValidationError(self, x, "time zone required for value %r." % self.value)
github sissaschool / xmlschema / xmlschema / validators / xsdbase.py View on Github external
def iter_errors(self, source, use_defaults=True, namespaces=None):
        """
        Creates an iterator for the errors generated by the validation of an XML data
        against the XSD schema/component instance.

        :param source: the source of XML data. For a schema can be a path \
        to a file or an URI of a resource or an opened file-like object or an Element Tree \
        instance or a string containing XML data. For other XSD components can be a string \
        for an attribute or a simple type validators, or an ElementTree's Element otherwise.
        :param use_defaults: Use schema's default values for filling missing data.
        :param namespaces: is an optional mapping from namespace prefix to URI.
        """
        for result in self.iter_decode(source, use_defaults=use_defaults, namespaces=namespaces):
            if isinstance(result, XMLSchemaValidationError):
                yield result
            else:
                del result
github sissaschool / xmlschema / xmlschema / validators / schema.py View on Github external
if root_only:
                # Tell to iterfind to catch namespace events and update map
                namespaces.clear()

        for elem in source.iterfind(path, namespaces):
            xsd_element = schema.get_element(elem.tag, schema_path, namespaces)
            if xsd_element is None:
                if XSI_TYPE in elem.attrib:
                    xsd_element = self.create_element(name=elem.tag)
                else:
                    reason = "{!r} is not an element of the schema".format(elem)
                    yield schema.validation_error('lax', reason, elem, source, namespaces)
                    return

            for result in xsd_element.iter_decode(elem, **kwargs):
                if isinstance(result, XMLSchemaValidationError):
                    yield result
                else:
                    del result

        # Check unresolved IDREF values
        for k, v in id_map.items():
            if isinstance(v, XMLSchemaValidationError):
                yield v
            elif v == 0:
                yield self.validation_error('lax', "IDREF %r not found in XML document" % k, source.root)
github sissaschool / xmlschema / xmlschema / validators / facets.py View on Github external
def prohibited_timezone_validator(self, x):
        if x.tzinfo is not None:
            yield XMLSchemaValidationError(self, x, "time zone prohibited for value %r." % self.value)
github sissaschool / xmlschema / xmlschema / validators / exceptions.py View on Github external
except (ValueError, TypeError):
                elem_as_string = repr(self.elem)

            if hasattr(self.elem, 'sourceline'):
                msg.append("Instance (line %r):\n\n%s\n" % (self.elem.sourceline, elem_as_string))
            else:
                msg.append("Instance:\n\n%s\n" % elem_as_string)
        if self.path is not None:
            msg.append("Path: %s\n" % self.path)
        return '\n'.join(msg)

    if PY3:
        __str__ = __unicode__


class XMLSchemaDecodeError(XMLSchemaValidationError):
    """
    Raised when an XML data string is not decodable to a Python object.

    :param validator: the XSD validator.
    :type validator: XsdValidator or function
    :param obj: the not validated XML data.
    :type obj: Element or tuple or str or list or int or float or bool
    :param decoder: the XML data decoder.
    :type decoder: type or function
    :param reason: the detailed reason of failed validation.
    :type reason: str or unicode
    :param source: the XML resource that contains the error.
    :type source: XMLResource
    :param namespaces: is an optional mapping from namespace prefix to URI.
    :type namespaces: dict
    """
github sissaschool / xmlschema / xmlschema / validators / groups.py View on Github external
def check_dynamic_context(self, elem, xsd_element, model_element, converter):
        if model_element is not xsd_element:
            if 'substitution' in model_element.block \
                    or xsd_element.type.is_blocked(model_element):
                raise XMLSchemaValidationError(
                    model_element, elem, "substitution of %r is blocked" % model_element
                )

        alternatives = ()
        if isinstance(xsd_element, XsdAnyElement):
            if xsd_element.process_contents == 'skip':
                return

            try:
                xsd_element = self.maps.lookup_element(elem.tag)
            except LookupError:
                try:
                    type_name = elem.attrib[XSI_TYPE].strip()
                except KeyError:
                    return
                else:
github sissaschool / xmlschema / xmlschema / validators / facets.py View on Github external
def validator(self, x):
        if x > self.value:
            yield XMLSchemaValidationError(self, x, "value has to be lesser or equal than %r." % self.value)
github sissaschool / xmlschema / xmlschema / validators / elements.py View on Github external
def check_dynamic_context(self, elem, **kwargs):
        try:
            locations = kwargs['locations']
        except KeyError:
            return

        for ns, url in etree_iter_location_hints(elem):
            if ns not in locations:
                locations[ns] = url
            elif locations[ns] is None:
                reason = "schemaLocation declaration after namespace start"
                raise XMLSchemaValidationError(self, elem, reason)

            if ns == self.target_namespace:
                schema = self.schema.include_schema(url, self.schema.base_url)
            else:
                schema = self.schema.import_namespace(ns, url, self.schema.base_url)

            if not schema.built:
                reason = "dynamic loaded schema change the assessment"
                raise XMLSchemaValidationError(self, elem, reason)

        if elem.attrib:
            for name in elem.attrib:
                if name[0] == '{':
                    ns = get_namespace(name)
                    if ns not in locations:
                        locations[ns] = None