Ticket #22943: validator_patch.diff

File validator_patch.diff, 6.2 KB (added by antialiasis@…, 10 years ago)

Use strings rather than precompiled regexes for built-in RegexValidator instances.

  • django/core/validators.py

    diff --git a/django/core/validators.py b/django/core/validators.py
    index 1e599ec..e33ab9e 100644
    a b class EmailValidator(object):  
    181181
    182182validate_email = EmailValidator()
    183183
    184 slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')
     184slug_re = r'^[-a-zA-Z0-9_]+$'
    185185validate_slug = RegexValidator(slug_re, _("Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens."), 'invalid')
    186186
    187 ipv4_re = re.compile(r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$')
     187ipv4_re = r'^(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}$'
    188188validate_ipv4_address = RegexValidator(ipv4_re, _('Enter a valid IPv4 address.'), 'invalid')
    189189
    190190
    def ip_address_validators(protocol, unpack_ipv4):  
    225225        raise ValueError("The protocol '%s' is unknown. Supported: %s"
    226226                         % (protocol, list(ip_address_validator_map)))
    227227
    228 comma_separated_int_list_re = re.compile('^[\d,]+$')
     228comma_separated_int_list_re = '^[\d,]+$'
    229229validate_comma_separated_integer_list = RegexValidator(comma_separated_int_list_re, _('Enter only digits separated by commas.'), 'invalid')
    230230
    231231
  • tests/validators/tests.py

    diff --git a/tests/validators/tests.py b/tests/validators/tests.py
    index f586270..62984be 100644
    a b from django.core.validators import (  
    1414    validate_ipv46_address, validate_ipv4_address, validate_ipv6_address,
    1515    validate_slug,
    1616)
     17from django.db.migrations.writer import MigrationWriter
    1718from django.test.utils import str_prefix
    1819
    1920
    class TestValidatorEquality(TestCase):  
    352353            MinValueValidator(45),
    353354            MinValueValidator(11),
    354355        )
     356
     357
     358class TestValidatorSerializability(TestCase):
     359    """
     360    Tests that all built-in class-based validators are serializable and
     361    thus usable in migrated apps (#22943).
     362
     363    Function-based validators are always deconstructible and don't have
     364    to be worried about.
     365    """
     366    def test_class_validator_serializability(self):
     367        def test_serializability(validator, name):
     368            try:
     369                MigrationWriter.serialize(validator)
     370            except ValueError:
     371                self.fail(u"Serialization of %s failed" % name)
     372
     373        validators = [
     374            (EmailValidator(), 'EmailValidator'),
     375            (MaxLengthValidator(20), 'MaxLengthValidator'),
     376            (MaxValueValidator(20), 'MaxValueValidator'),
     377            (MinLengthValidator(20), 'MinLengthValidator'),
     378            (MinValueValidator(20), 'MinValueValidator'),
     379            (RegexValidator(r'^\w+$'), 'RegexValidator'),
     380            (URLValidator(), 'URLValidator'),
     381            (validate_comma_separated_integer_list, 'validate_comma_separated_integer_list'),
     382            (validate_email, 'validate_email'),
     383            (validate_ipv4_address, 'validate_ipv4_address'),
     384            (validate_slug, 'validate_slug')
     385        ]
     386        for validator, name in validators:
     387            test_serializability(validator, name)
Back to Top