Skip to main content

EmailValidator

This class provides a mechanism for validating email addresses against RFC-compliant regular expressions and length constraints. It supports validation for both standard domain names and literal IP addresses, while allowing for a customizable allowlist of domains. Users can initialize the validator with custom error messages and codes to integrate with specific validation frameworks.

Attributes

AttributeTypeDescription
messagestring = Enter a valid email address.Error message used when validation fails, defaulting to "Enter a valid email address."
codestring = invalidThe error code used in the ValidationError raised when validation fails.
hostname_reregex patternRegular expression pattern used to validate the hostname portion of the domain.
domain_reregex patternRegular expression pattern used to validate the domain structure.
tld_no_fqdn_reregex patternRegular expression pattern used to validate the top-level domain when not using a fully qualified domain name.
user_regexregex patternRegular expression pattern used to validate the local part of the email address before the @ symbol.
domain_regexregex patternCompiled regular expression combining hostname, domain, and TLD patterns to validate the domain part of the email.
literal_regexregex patternRegular expression pattern used to validate IPv4 or IPv6 address literals enclosed in square brackets.
domain_allowlistlist of strings = ["localhost"]A list of domain names that are considered valid even if they do not match the standard domain regular expressions.

Constructor

Signature

def EmailValidator(
message: string = None,
code: string = None,
allowlist: list = None
)

Parameters

NameTypeDescription
messagestring = NoneThe error message to be used when validation fails.
codestring = NoneThe error code to be used when validation fails.
allowlistlist = NoneA list of domains that are explicitly allowed.

Methods


validate_domain_part()

@classmethod
def validate_domain_part(
domain_part: string
) - > boolean

Checks if the domain portion of an email is a valid hostname, a valid IP literal, or matches an allowed domain.

Parameters

NameTypeDescription
domain_partstringThe substring of the email address following the '@' symbol to be checked for validity.

Returns

TypeDescription
booleanTrue if the domain part is a valid hostname or IP address, False otherwise.