Skip to content

Instantly share code, notes, and snippets.

@exit99
Created November 17, 2016 22:18
Show Gist options
  • Save exit99/1b776e6354994380c465be602b6f7235 to your computer and use it in GitHub Desktop.
Save exit99/1b776e6354994380c465be602b6f7235 to your computer and use it in GitHub Desktop.
class _BaseDomainNameValidator(BaseValidator):
def __call__(self, form, field):
labels = get_labels(value)
if not labels:
return False
is_valid = True
for index, label in enumerate(labels):
if index == 0 and label == '*':
continue
if not label and index + 1 != len(labels):
is_valid = False
label_str = get_label_str(label)
if re.search(self.regex, test_str):
field.errors.append('Must be a valid domain name.')
is_valid = False
return is_valid
def get_labels(self, value):
if not value or value in ['@', '*']:
return []
return value.split('.')
def get_label_str(label):
"""Can be overridden"""
return label
class DomainNameValidator(BaseValidator):
regex = '[^a-zA-Z0-9-_]'
class TxtRecordDomainNameValidator(BaseValidator):
regex = '[^a-zA-Z0-9-]'
def get_label_str(self, label):
return label[1:] if label.startswith('_') else label
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment