import re import django.core.validators from django.core.exceptions import ValidationError from django.core.validators import RegexValidator from django.utils.translation import gettext_lazy as _, ngettext_lazy def validate_first_letter_not_number(value): if len(value) > 0 and value[0].isdigit(): raise ValidationError(_('the first letter cannot be a number')) def end_with_letter_or_number(value): if len(value) > 0 and not value[-1].isalnum(): raise ValidationError(_('must end with a letter or a number')) class NoDoubleSpecialValidator(RegexValidator): regex = re.compile(r'[-_.]{2,}') inverse_match = True message = _('must not contain a sequence of 2 or more special chars (.-_)') class DoesNotEndWithConfusingSuffixValidator(RegexValidator): regex = re.compile(r'\.(js|json|css|htm|html|xml|jpg|jpeg|png|gif|bmp|ico|tif|tiff|woff)$') inverse_match = True message = _('must not end with a confusing suffix like .json or .png etc.') class MinLengthValidator(django.core.validators.MinLengthValidator): message = ngettext_lazy( 'Ensure this value has at least %(limit_value)d character.', 'Ensure this value has at least %(limit_value)d characters.', 'limit_value') class MaxLengthValidator(django.core.validators.MaxLengthValidator): message = ngettext_lazy( 'Ensure this value has at most %(limit_value)d character.', 'Ensure this value has at most %(limit_value)d characters.', 'limit_value')