Python Data Validation for Humans™ - comprehensive validation library for various data types without schema definitions
Validators for internet-related data including domains, email addresses, URLs, hostnames, IP addresses, and network hardware identifiers. These validators support various RFC standards and provide extensive configuration options.
Validates domain names with support for TLD checking and various RFC standards.
def domain(value: str, /, *, consider_tld: bool = False, rfc_1034: bool = False, rfc_2782: bool = False) -> Union[Literal[True], ValidationError]:
"""
Validate domain names.
Parameters:
- value: Domain name string to validate
- consider_tld: Restrict to IANA-approved top-level domains
- rfc_1034: Allow trailing dot in domain name (RFC 1034)
- rfc_2782: Domain name is service record type (RFC 2782)
Returns:
True if valid domain, ValidationError otherwise
"""Comprehensive email validation supporting IPv4/IPv6 addresses and various hostname formats.
def email(value: str, /, *, ipv6_address: bool = False, ipv4_address: bool = False, simple_host: bool = False, rfc_1034: bool = False, rfc_2782: bool = False) -> Union[Literal[True], ValidationError]:
"""
Validate email addresses per RFC standards.
Parameters:
- value: Email address string to validate
- ipv6_address: Allow IPv6 addresses in domain part
- ipv4_address: Allow IPv4 addresses in domain part
- simple_host: Allow simple hostnames (no dots)
- rfc_1034: Allow trailing dot in domain (RFC 1034)
- rfc_2782: Domain is service record type (RFC 2782)
Returns:
True if valid email, ValidationError otherwise
Supports extended Latin characters in local part.
Validates both domain names and IP addresses in domain part.
"""Full URL validation with scheme checking, authentication, and query parameter validation.
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] = _validate_scheme) -> Union[Literal[True], ValidationError]:
"""
Validate URLs with comprehensive scheme and component checking.
Parameters:
- value: URL string to validate
- skip_ipv6_addr: Reject IPv6 addresses in hostname
- skip_ipv4_addr: Reject IPv4 addresses in hostname
- may_have_port: Allow port numbers in hostname
- simple_host: Allow simple hostnames without dots
- strict_query: Strict query string parsing
- consider_tld: Restrict to IANA TLDs
- private: Require private/public IP addresses (IPv4/IPv6 only)
- rfc_1034: Allow trailing dots (RFC 1034)
- rfc_2782: Service record hostnames (RFC 2782)
- validate_scheme: Custom scheme validation function
Returns:
True if valid URL, ValidationError otherwise
Supported schemes: http, https, ftp, ftps, ssh, sftp, git, irc, rtmp, rtmps, rtsp, telnet
Validates authentication segments, paths, query parameters, and fragments.
"""Validates hostnames including IPv4/IPv6 addresses, ports, and various hostname formats.
def hostname(value: str, /, *, skip_ipv6_addr: bool = False, skip_ipv4_addr: bool = False, may_have_port: bool = True, maybe_simple: bool = True, consider_tld: bool = False, private: Optional[bool] = None, rfc_1034: bool = False, rfc_2782: bool = False) -> Union[Literal[True], ValidationError]:
"""
Validate hostnames including domain names and IP addresses.
Parameters:
- value: Hostname string to validate
- skip_ipv6_addr: Reject IPv6 addresses
- skip_ipv4_addr: Reject IPv4 addresses
- may_have_port: Allow port numbers
- maybe_simple: Allow simple hostnames (no dots)
- consider_tld: Restrict to IANA TLDs
- private: Require private/public IP addresses
- rfc_1034: Allow trailing dots (RFC 1034)
- rfc_2782: Service record format (RFC 2782)
Returns:
True if valid hostname, ValidationError otherwise
"""Validates IPv4 addresses with CIDR notation support and private/public classification.
def ipv4(value: str, /, *, cidr: bool = True, strict: bool = False, private: Optional[bool] = None, host_bit: bool = True) -> Union[Literal[True], ValidationError]:
"""
Validate IPv4 addresses.
Parameters:
- value: IPv4 address string to validate
- cidr: Allow CIDR notation (e.g., 192.168.1.0/24)
- strict: Strict validation mode
- private: Require private (True) or public (False) addresses
- host_bit: Allow host bits in CIDR notation
Returns:
True if valid IPv4, ValidationError otherwise
Supports standard dotted decimal notation.
Validates CIDR blocks when enabled.
Distinguishes private vs public address spaces.
"""Validates IPv6 addresses with CIDR notation support.
def ipv6(value: str, /, *, cidr: bool = True, strict: bool = False, host_bit: bool = True) -> Union[Literal[True], ValidationError]:
"""
Validate IPv6 addresses.
Parameters:
- value: IPv6 address string to validate
- cidr: Allow CIDR notation (e.g., 2001:db8::/32)
- strict: Strict validation mode
- host_bit: Allow host bits in CIDR notation
Returns:
True if valid IPv6, ValidationError otherwise
Supports full and compressed IPv6 notation.
Handles IPv4-mapped IPv6 addresses.
Validates CIDR blocks when enabled.
"""Validates MAC (Media Access Control) addresses in standard formats.
def mac_address(value: str, /) -> Union[Literal[True], ValidationError]:
"""
Validate MAC addresses.
Parameters:
- value: MAC address string to validate
Returns:
True if valid MAC address, ValidationError otherwise
Supports formats:
- Colon-separated: 01:23:45:67:89:ab
- Hyphen-separated: 01-23-45-67-89-ab
- Dot-separated: 0123.4567.89ab
- No separators: 0123456789ab
"""import validators
# Domain validation
validators.domain('example.com') # True
validators.domain('sub.example.com') # True
validators.domain('example.co.uk', consider_tld=True) # True
# Email validation
validators.email('user@example.com') # True
validators.email('user@[192.168.1.1]', ipv4_address=True) # True
validators.email('user@[2001:db8::1]', ipv6_address=True) # True
# URL validation
validators.url('https://example.com') # True
validators.url('https://user:pass@example.com:8080/path?query=1#frag') # True
validators.url('ftp://ftp.example.com/file.txt') # True
# IP address validation
validators.ipv4('192.168.1.1') # True
validators.ipv4('192.168.1.0/24') # True (CIDR)
validators.ipv4('10.0.0.1', private=True) # True (private IP)
validators.ipv6('2001:db8::1') # True
validators.ipv6('::1') # True (loopback)
validators.ipv6('2001:db8::/32') # True (CIDR)
# MAC address validation
validators.mac_address('01:23:45:67:89:ab') # True
validators.mac_address('01-23-45-67-89-ab') # True
validators.mac_address('0123.4567.89ab') # TrueInstall with Tessl CLI
npx tessl i tessl/pypi-validators