Skip to main content

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

AttributeTypeDescription
ipv4_restring = r"(?:025[0-5]
ipv6_restring = r"[[0-9a-f:.]+]"Regular expression pattern used for initial identification of IPv6 addresses enclosed in brackets.
hostname_restringRegular expression pattern for validating the hostname part of a domain.
domain_restringRegular expression pattern for validating the domain name component.
tld_restringRegular expression pattern for validating the top-level domain (TLD) part of a URL.
host_restringCombined regular expression pattern that matches a hostname, domain, TLD, or localhost.
regexcompiled regexThe compiled regular expression used to validate the overall structure of the URL, including scheme, authentication, host, port, and path.
messagestring = _("Enter a valid URL.")Error message returned when the URL validation fails.
schemeslist of strings = ["http", "https", "ftp", "ftps"]A list of permitted URL schemes, such as 'http' or 'https', used to restrict valid protocols.
unsafe_charsfrozenset = frozenset("\t\r\n")A set of characters considered unsafe that will trigger a validation error if present in the URL.
max_lengthinteger = MAX_URL_LENGTHThe maximum allowed character length for a URL string.

Constructor

Signature

def URLValidator(
schemes: list = None,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
schemeslist = NoneA list of allowed URL schemes (e.g., ['http', 'https']). If None, the default schemes are used.
**kwargsdictArbitrary keyword arguments passed to the parent RegexValidator constructor.