2020-06-11 00:54:34 +02:00
|
|
|
import re
|
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2017-01-21 08:27:36 +01:00
|
|
|
|
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
def validate_domain(domain: str | None) -> None:
|
2017-01-21 08:27:36 +01:00
|
|
|
if domain is None or len(domain) == 0:
|
|
|
|
raise ValidationError(_("Domain can't be empty."))
|
2021-02-12 08:20:45 +01:00
|
|
|
if "." not in domain:
|
2017-01-21 08:27:36 +01:00
|
|
|
raise ValidationError(_("Domain must have at least one dot (.)"))
|
2018-04-23 18:28:42 +02:00
|
|
|
if len(domain) > 255:
|
|
|
|
raise ValidationError(_("Domain is too long"))
|
2021-02-12 08:20:45 +01:00
|
|
|
if domain[0] == "." or domain[-1] == ".":
|
2017-01-21 08:27:36 +01:00
|
|
|
raise ValidationError(_("Domain cannot start or end with a dot (.)"))
|
2021-02-12 08:20:45 +01:00
|
|
|
for subdomain in domain.split("."):
|
2017-01-21 08:27:36 +01:00
|
|
|
if not subdomain:
|
|
|
|
raise ValidationError(_("Consecutive '.' are not allowed."))
|
2021-02-12 08:20:45 +01:00
|
|
|
if subdomain[0] == "-" or subdomain[-1] == "-":
|
2017-01-21 08:27:36 +01:00
|
|
|
raise ValidationError(_("Subdomains cannot start or end with a '-'."))
|
2024-04-26 20:30:22 +02:00
|
|
|
if not re.match(r"^[a-z0-9-]*$", subdomain):
|
2017-01-21 08:27:36 +01:00
|
|
|
raise ValidationError(_("Domain can only have letters, numbers, '.' and '-'s."))
|