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
| Attribute | Type | Description |
|---|---|---|
| message | string = Enter a valid email address. | Error message used when validation fails, defaulting to "Enter a valid email address." |
| code | string = invalid | The error code used in the ValidationError raised when validation fails. |
| hostname_re | regex pattern | Regular expression pattern used to validate the hostname portion of the domain. |
| domain_re | regex pattern | Regular expression pattern used to validate the domain structure. |
| tld_no_fqdn_re | regex pattern | Regular expression pattern used to validate the top-level domain when not using a fully qualified domain name. |
| user_regex | regex pattern | Regular expression pattern used to validate the local part of the email address before the @ symbol. |
| domain_regex | regex pattern | Compiled regular expression combining hostname, domain, and TLD patterns to validate the domain part of the email. |
| literal_regex | regex pattern | Regular expression pattern used to validate IPv4 or IPv6 address literals enclosed in square brackets. |
| domain_allowlist | list 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
| Name | Type | Description |
|---|---|---|
| message | string = None | The error message to be used when validation fails. |
| code | string = None | The error code to be used when validation fails. |
| allowlist | list = None | A 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
| Name | Type | Description |
|---|---|---|
| domain_part | string | The substring of the email address following the '@' symbol to be checked for validity. |
Returns
| Type | Description |
|---|---|
boolean | True if the domain part is a valid hostname or IP address, False otherwise. |