URLValidator
This class provides comprehensive validation for URLs, ensuring they conform to standard formatting rules including scheme, authentication, host, port, and resource path. It supports customizable protocol schemes and performs specific checks for IPv4, IPv6, and domain-based hostnames. Additionally, the validator enforces length constraints and identifies unsafe characters to ensure the input is a secure and valid URL.
Attributes
| Attribute | Type | Description |
|---|---|---|
| ipv4_re | string = r"(?:0 | 25[0-5] |
| ipv6_re | string = r"[[0-9a-f:.]+]" | Regular expression pattern used for initial identification of IPv6 addresses enclosed in brackets. |
| hostname_re | string | Regular expression pattern for validating the hostname part of a domain. |
| domain_re | string | Regular expression pattern for validating the domain name component. |
| tld_re | string | Regular expression pattern for validating the top-level domain (TLD) part of a URL. |
| host_re | string | Combined regular expression pattern that matches a hostname, domain, TLD, or localhost. |
| regex | compiled regex | The compiled regular expression used to validate the overall structure of the URL, including scheme, authentication, host, port, and path. |
| message | string = _("Enter a valid URL.") | Error message returned when the URL validation fails. |
| schemes | list of strings = ["http", "https", "ftp", "ftps"] | A list of permitted URL schemes, such as 'http' or 'https', used to restrict valid protocols. |
| unsafe_chars | frozenset = frozenset("\t\r\n") | A set of characters considered unsafe that will trigger a validation error if present in the URL. |
| max_length | integer = MAX_URL_LENGTH | The maximum allowed character length for a URL string. |
Constructor
Signature
def URLValidator(
schemes: list = None,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| schemes | list = None | A list of allowed URL schemes (e.g., ['http', 'https']). If None, the default schemes are used. |
| **kwargs | dict | Arbitrary keyword arguments passed to the parent RegexValidator constructor. |