Example #1
0
    def check_url(value):
        validate(str, value)
        parsed = urlparse(value)
        if not parsed.netloc:
            raise ValidationError(
                "{value} is not a valid URL",
                value=repr(value),
                schema="url",
            )

        for name, schema in attributes.items():
            if not hasattr(parsed, name):
                raise ValidationError(
                    "Invalid URL attribute {name}",
                    name=repr(name),
                    schema="url",
                )

            try:
                validate(schema, getattr(parsed, name))
            except ValidationError as err:
                raise ValidationError(
                    "Unable to validate URL attribute {name}",
                    name=repr(name),
                    schema="url",
                    context=err,
                )

        return True
Example #2
0
    def contains_str(value):
        validate(str, value)
        if string not in value:
            raise ValidationError(
                "{value} does not contain {string}",
                value=repr(value),
                string=repr(string),
                schema="contains",
            )

        return True
Example #3
0
    def ends_with(value):
        validate(str, value)
        if not value.endswith(string):
            raise ValidationError(
                "{value} does not end with {string}",
                value=repr(value),
                string=repr(string),
                schema="endswith",
            )

        return True
Example #4
0
    def starts_with(value):
        validate(str, value)
        if not value.startswith(string):
            raise ValidationError(
                "{value} does not start with {string}",
                value=repr(value),
                string=repr(string),
                schema="startswith",
            )

        return True
Example #5
0
    def xpath_find(value):
        validate(iselement, value)
        value = value.find(xpath)
        if value is None:
            raise ValidationError(
                "XPath {xpath} did not return an element",
                xpath=repr(xpath),
                schema="xml_find",
            )

        return validate(iselement, value)
Example #6
0
 def transform_xpath(value):
     validate(iselement, value)
     return value.xpath(xpath) or None
Example #7
0
 def xpath_findall(value):
     validate(iselement, value)
     return value.findall(xpath)