Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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

  • Loading branch information...
commit a68f32579145dfbd51d87e14f9b5e04c770f9081 1 parent 3f1a008
Andrew Godwin authored
9  django/core/validators.py
@@ -40,6 +40,9 @@ def __call__(self, value):
40 40
         if not self.regex.search(force_text(value)):
41 41
             raise ValidationError(self.message, code=self.code)
42 42
 
  43
+    def __eq__(self, other):
  44
+        return isinstance(other, RegexValidator) and (self.regex == other.regex) and (self.message == other.message) and (self.code == other.code)
  45
+
43 46
 
44 47
 @deconstructible
45 48
 class URLValidator(RegexValidator):
@@ -139,6 +142,9 @@ def __call__(self, value):
139 142
                 pass
140 143
             raise ValidationError(self.message, code=self.code)
141 144
 
  145
+    def __eq__(self, other):
  146
+        return isinstance(other, EmailValidator) and (self.domain_whitelist == other.domain_whitelist) and (self.message == other.message) and (self.code == other.code)
  147
+
142 148
 validate_email = EmailValidator()
143 149
 
144 150
 slug_re = re.compile(r'^[-a-zA-Z0-9_]+$')
@@ -205,6 +211,9 @@ def __call__(self, value):
205 211
         if self.compare(cleaned, self.limit_value):
206 212
             raise ValidationError(self.message, code=self.code, params=params)
207 213
 
  214
+    def __eq__(self, other):
  215
+        return isinstance(other, self.__class__) and (self.limit_value == other.limit_value) and (self.message == other.message) and (self.code == other.code)
  216
+
208 217
 
209 218
 @deconstructible
210 219
 class MaxValueValidator(BaseValidator):
56  tests/validators/tests.py
@@ -244,3 +244,59 @@ def test_message_dict(self):
244 244
     name, method = create_simple_test_method(validator, expected, value, test_counter)
245 245
     setattr(TestSimpleValidators, name, method)
246 246
     test_counter += 1
  247
+
  248
+
  249
+class TestValidatorEquality(TestCase):
  250
+    """
  251
+    Tests that validators have valid equality operators (#21638)
  252
+    """
  253
+
  254
+    def test_regex_equality(self):
  255
+        self.assertEqual(
  256
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
  257
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
  258
+        )
  259
+        self.assertNotEqual(
  260
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
  261
+            RegexValidator(r'^(?:[0-9\.\-]*)://'),
  262
+        )
  263
+        self.assertEqual(
  264
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
  265
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
  266
+        )
  267
+        self.assertNotEqual(
  268
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh", "invalid"),
  269
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
  270
+        )
  271
+        self.assertNotEqual(
  272
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://', "oh noes", "invalid"),
  273
+            RegexValidator(r'^(?:[a-z0-9\.\-]*)://'),
  274
+        )
  275
+
  276
+    def test_email_equality(self):
  277
+        self.assertEqual(
  278
+            EmailValidator(),
  279
+            EmailValidator(),
  280
+        )
  281
+        self.assertNotEqual(
  282
+            EmailValidator(message="BAD EMAIL"),
  283
+            EmailValidator(),
  284
+        )
  285
+        self.assertEqual(
  286
+            EmailValidator(message="BAD EMAIL", code="bad"),
  287
+            EmailValidator(message="BAD EMAIL", code="bad"),
  288
+        )
  289
+
  290
+    def test_basic_equality(self):
  291
+        self.assertEqual(
  292
+            MaxValueValidator(44),
  293
+            MaxValueValidator(44),
  294
+        )
  295
+        self.assertNotEqual(
  296
+            MaxValueValidator(44),
  297
+            MinValueValidator(44),
  298
+        )
  299
+        self.assertNotEqual(
  300
+            MinValueValidator(45),
  301
+            MinValueValidator(11),
  302
+        )

0 notes on commit a68f325

Please sign in to comment.
Something went wrong with that request. Please try again.