Skip to content

Validators

is_numeric = RegexValidator('^[0-9]*$', 'Only numbers, leading zeros are ok.') module-attribute

RegexValidator: Validates that a string contains only numeric characters.

This validator ensures that the input string consists solely of digits (0-9). Leading zeros are permitted in the input.

Examples:

Valid inputs: "123", "001", "0", "" Invalid inputs: "12a", "-1", "1.0", "1,000"

Attributes:

Name Type Description
regex str

The regular expression pattern "^[0-9]*$"

message str

The error message displayed when validation fails

django_validate_email(email, whitelist_domains=None)

Validates an email address using Django's validator.

Parameters:

Name Type Description Default
email str

Email address to validate.

required
whitelist_domains Optional[List[str]]

List of allowed email domains.

None

Returns:

Type Description
str | None

Optional[str]: The validated email address if valid, None if invalid.

Examples:

>>> django_validate_email("valid@email.com")
'valid@email.com'
>>> django_validate_email("valid@email.com", whitelist_domains=["email.com"])
'valid@email.com'
>>> print(django_validate_email("valid@email.com", whitelist_domains=["other.com"]))
None
Source code in django_util/validators.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def django_validate_email(
    email: str,
    whitelist_domains: list[str] | None = None,
) -> str | None:
    """Validates an email address using Django's validator.

    Args:
        email (str): Email address to validate.
        whitelist_domains (Optional[List[str]]): List of allowed email domains.

    Returns:
        Optional[str]: The validated email address if valid, None if invalid.

    Examples:
        >>> django_validate_email("valid@email.com")
        'valid@email.com'
        >>> django_validate_email("valid@email.com", whitelist_domains=["email.com"])
        'valid@email.com'
        >>> print(django_validate_email("valid@email.com", whitelist_domains=["other.com"]))
        None
    """
    from django.core.validators import validate_email

    try:
        validate_email(email)
        if whitelist_domains:
            domain = email.split("@")[1].lower()
            if domain not in whitelist_domains:
                logger.warning(f"Email domain {domain} not in whitelist")
                return None
        return email
    except Exception as e:
        logger.debug(f"Email validation failed: {e}")
        return None

django_validate_phone(phone, region=None)

Validates and formats a phone number using Django's phone number field.

Parameters:

Name Type Description Default
phone str

Phone number to validate.

required
region Optional[str]

Region code for parsing local numbers. Defaults to None.

None

Returns:

Type Description
str | None

Optional[str]: The phone number in E.164 format if valid, None if invalid.

Examples:

>>> django_validate_phone("+1234567890")
'+1234567890'
>>> print(django_validate_phone("invalid"))
None
Note

Requires django-phonenumber-field package to be installed.

Source code in django_util/validators.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
def django_validate_phone(
    phone: str,
    region: str | None = None,
) -> str | None:
    """Validates and formats a phone number using Django's phone number field.

    Args:
        phone (str): Phone number to validate.
        region (Optional[str], optional): Region code for parsing local numbers. Defaults to None.

    Returns:
        Optional[str]: The phone number in E.164 format if valid, None if invalid.

    Examples:
        >>> django_validate_phone("+1234567890")
        '+1234567890'
        >>> print(django_validate_phone("invalid"))
        None

    Note:
        Requires django-phonenumber-field package to be installed.
    """
    from phonenumber_field.phonenumber import PhoneNumber

    try:
        return PhoneNumber.from_string(phone).as_e164
    except Exception:
        try:
            return PhoneNumber.from_string(phone, region=region).as_e164
        except Exception as e:
            logger.error(e)
            return None

django_validate_url(url, allowed_schemes=None)

Validates a URL using Django's URL validator.

Parameters:

Name Type Description Default
url str

URL to validate.

required
allowed_schemes Optional[List[str]]

List of allowed URL schemes (e.g., ['http', 'https']).

None

Returns:

Type Description
str | None

Optional[str]: The validated URL if valid, None if invalid.

Source code in django_util/validators.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def django_validate_url(
    url: str,
    allowed_schemes: list[str] | None = None,
) -> str | None:
    """Validates a URL using Django's URL validator.

    Args:
        url (str): URL to validate.
        allowed_schemes (Optional[List[str]]): List of allowed URL schemes (e.g., ['http', 'https']).

    Returns:
        Optional[str]: The validated URL if valid, None if invalid.
    """
    from django.core.validators import URLValidator

    validator = URLValidator(
        schemes=allowed_schemes if allowed_schemes else ["http", "https", "ftp", "ftps"]
    )
    try:
        validator(url)
        return url
    except Exception as e:
        logger.debug(f"URL validation failed: {e}")
        return None