Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #21638: Validators are now comparable, stops infinite user mig'ns

  • Loading branch information...
commit a68f32579145dfbd51d87e14f9b5e04c770f9081 1 parent 3f1a008
@andrewgodwin andrewgodwin authored
Showing with 65 additions and 0 deletions.
  1. +9 −0 django/core/validators.py
  2. +56 −0 tests/validators/tests.py
View
9 django/core/validators.py
@@ -40,6 +40,9 @@ def __call__(self, value):
if not self.regex.search(force_text(value)):
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
class URLValidator(RegexValidator):
@@ -139,6 +142,9 @@ def __call__(self, value):
pass
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()
slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')
@@ -205,6 +211,9 @@ def __call__(self, value):
if self.compare(cleaned, self.limit_value):
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
class MaxValueValidator(BaseValidator):
View
56 tests/validators/tests.py
@@ -244,3 +244,59 @@ def test_message_dict(self):
name, method = create_simple_test_method(validator, expected, value, test_counter)
setattr(TestSimpleValidators, name, method)
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),
+ )
Please sign in to comment.
Something went wrong with that request. Please try again.