validators.url

URL.

@validator
def url( value: str, /, *, skip_ipv6_addr: bool = False, skip_ipv4_addr: bool = False, may_have_port: bool = True, simple_host: bool = False, strict_query: bool = True, consider_tld: bool = False, private: Optional[bool] = None, rfc_1034: bool = False, rfc_2782: bool = False, validate_scheme: Callable[[str], bool] = <function _validate_scheme>):

Return whether or not given value is a valid URL.

This validator was originally inspired from URL validator of dperini. The following diagram is from urlly::

    foo://admin:hunter1@example.com:8042/over/there?name=ferret#nose
    \_/   \___/ \_____/ \_________/ \__/\_________/ \_________/ \__/
     |      |       |       |        |       |          |         |
  scheme username password hostname port    path      query    fragment
Examples:
>>> url('http://duck.com')
True
>>> url('ftp://foobar.dk')
True
>>> url('http://10.0.0.1')
True
>>> url('http://example.com/">user@example.com')
ValidationError(func=url, args={'value': 'http://example.com/">user@example.com'})
Arguments:
  • value: URL string to validate.
  • skip_ipv6_addr: When URL string cannot contain an IPv6 address.
  • skip_ipv4_addr: When URL string cannot contain an IPv4 address.
  • may_have_port: URL string may contain port number.
  • simple_host: URL string maybe only hyphens and alpha-numerals.
  • strict_query: Fail validation on query string parsing error.
  • consider_tld: Restrict domain to TLDs allowed by IANA.
  • private: Embedded IP address is public if False, private/local if True.
  • rfc_1034: Allow trailing dot in domain/host name. Ref: RFC 1034.
  • rfc_2782: Domain/Host name is of type service record. Ref: RFC 2782.
  • validate_scheme: Function that validates URL scheme.
Returns:

(Literal[True]): If value is a valid url. (ValidationError): If value is an invalid url.