2017-01-21 08:27:36 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2019-07-24 21:02:57 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
import re
|
|
|
|
|
2019-07-24 21:02:57 +02:00
|
|
|
def validate_domain(domain: Optional[str]) -> None:
|
2017-01-21 08:27:36 +01:00
|
|
|
if domain is None or len(domain) == 0:
|
|
|
|
raise ValidationError(_("Domain can't be empty."))
|
|
|
|
if '.' not in domain:
|
|
|
|
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"))
|
2017-01-21 08:27:36 +01:00
|
|
|
if domain[0] == '.' or domain[-1] == '.':
|
|
|
|
raise ValidationError(_("Domain cannot start or end with a dot (.)"))
|
|
|
|
for subdomain in domain.split('.'):
|
|
|
|
if not subdomain:
|
|
|
|
raise ValidationError(_("Consecutive '.' are not allowed."))
|
|
|
|
if subdomain[0] == '-' or subdomain[-1] == '-':
|
|
|
|
raise ValidationError(_("Subdomains cannot start or end with a '-'."))
|
|
|
|
if not re.match('^[a-z0-9-]*$', subdomain):
|
|
|
|
raise ValidationError(_("Domain can only have letters, numbers, '.' and '-'s."))
|