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
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
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
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
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)
def transform_xpath(value): validate(iselement, value) return value.xpath(xpath) or None
def xpath_findall(value): validate(iselement, value) return value.findall(xpath)