Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixed #21638: Validators are now comparable, stops infinite user mig'ns
  • Loading branch information
andrewgodwin committed Jan 19, 2014
1 parent 3f1a008 commit a68f325
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
9 changes: 9 additions & 0 deletions django/core/validators.py
Expand Up @@ -40,6 +40,9 @@ def __call__(self, value):
if not self.regex.search(force_text(value)): if not self.regex.search(force_text(value)):
raise ValidationError(self.message, code=self.code) raise ValidationError(self.message, code=self.code)


def __eq__(self, other):
return isinstance(other, RegexValidator) and (self.regex == other.regex) and (self.message == other.message) and (self.code == other.code)



@deconstructible @deconstructible
class URLValidator(RegexValidator): class URLValidator(RegexValidator):
Expand Down Expand Up @@ -139,6 +142,9 @@ def __call__(self, value):
pass pass
raise ValidationError(self.message, code=self.code) raise ValidationError(self.message, code=self.code)


def __eq__(self, other):
return isinstance(other, EmailValidator) and (self.domain_whitelist == other.domain_whitelist) and (self.message == other.message) and (self.code == other.code)

validate_email = EmailValidator() validate_email = EmailValidator()


slug_re = re.compile(r'^[-a-zA-Z0-9_]+$') slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')
Expand Down Expand Up @@ -205,6 +211,9 @@ def __call__(self, value):
if self.compare(cleaned, self.limit_value): if self.compare(cleaned, self.limit_value):
raise ValidationError(self.message, code=self.code, params=params) raise ValidationError(self.message, code=self.code, params=params)


def __eq__(self, other):
return isinstance(other, self.__class__) and (self.limit_value == other.limit_value) and (self.message == other.message) and (self.code == other.code)



@deconstructible @deconstructible
class MaxValueValidator(BaseValidator): class MaxValueValidator(BaseValidator):
Expand Down
56 changes: 56 additions & 0 deletions tests/validators/tests.py
Expand Up @@ -244,3 +244,59 @@ def test_message_dict(self):
name, method = create_simple_test_method(validator, expected, value, test_counter) name, method = create_simple_test_method(validator, expected, value, test_counter)
setattr(TestSimpleValidators, name, method) setattr(TestSimpleValidators, name, method)
test_counter += 1 test_counter += 1


class TestValidatorEquality(TestCase):
"""
Tests that validators have valid equality operators (#21638)
"""

def test_regex_equality(self):
self.assertEqual(
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
)
self.assertNotEqual(
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
RegexValidator(r'^(?:[0-9\.\-]*)://'),
)
self.assertEqual(
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
)
self.assertNotEqual(
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh", "invalid"),
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
)
self.assertNotEqual(
RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
)

def test_email_equality(self):
self.assertEqual(
EmailValidator(),
EmailValidator(),
)
self.assertNotEqual(
EmailValidator(message="BAD EMAIL"),
EmailValidator(),
)
self.assertEqual(
EmailValidator(message="BAD EMAIL", code="bad"),
EmailValidator(message="BAD EMAIL", code="bad"),
)

def test_basic_equality(self):
self.assertEqual(
MaxValueValidator(44),
MaxValueValidator(44),
)
self.assertNotEqual(
MaxValueValidator(44),
MinValueValidator(44),
)
self.assertNotEqual(
MinValueValidator(45),
MinValueValidator(11),
)

0 comments on commit a68f325

Please sign in to comment.