mirror of
				https://github.com/zulip/zulip.git
				synced 2025-11-04 05:53:43 +00:00 
			
		
		
		
	Fixes #2665. Regenerated by tabbott with `lint --fix` after a rebase and change in parameters. Note from tabbott: In a few cases, this converts technical debt in the form of unsorted imports into different technical debt in the form of our largest files having very long, ugly import sequences at the start. I expect this change will increase pressure for us to split those files, which isn't a bad thing. Signed-off-by: Anders Kaseorg <anders@zulip.com>
		
			
				
	
	
		
			24 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			24 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import re
 | 
						|
from typing import Optional
 | 
						|
 | 
						|
from django.core.exceptions import ValidationError
 | 
						|
from django.utils.translation import ugettext as _
 | 
						|
 | 
						|
 | 
						|
def validate_domain(domain: Optional[str]) -> None:
 | 
						|
    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 (.)"))
 | 
						|
    if len(domain) > 255:
 | 
						|
        raise ValidationError(_("Domain is too long"))
 | 
						|
    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."))
 |