Skip to content

Commit 3e7497a

Browse files
myiitimgraham
authored andcommitted
Fixed #28758 -- Fixed RangeMax/MinValueValidators crash with unbound ranges.
1 parent 1b7780e commit 3e7497a

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

django/contrib/postgres/validators.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ def __eq__(self, other):
6969

7070
class RangeMaxValueValidator(MaxValueValidator):
7171
def compare(self, a, b):
72-
return a.upper > b
72+
return a.upper is None or a.upper > b
7373
message = _('Ensure that this range is completely less than or equal to %(limit_value)s.')
7474

7575

7676
class RangeMinValueValidator(MinValueValidator):
7777
def compare(self, a, b):
78-
return a.lower < b
78+
return a.lower is None or a.lower < b
7979
message = _('Ensure that this range is completely greater than or equal to %(limit_value)s.')

tests/postgres_tests/test_ranges.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,18 +408,24 @@ class TestValidators(PostgreSQLTestCase):
408408
def test_max(self):
409409
validator = RangeMaxValueValidator(5)
410410
validator(NumericRange(0, 5))
411+
msg = 'Ensure that this range is completely less than or equal to 5.'
411412
with self.assertRaises(exceptions.ValidationError) as cm:
412413
validator(NumericRange(0, 10))
413-
self.assertEqual(cm.exception.messages[0], 'Ensure that this range is completely less than or equal to 5.')
414+
self.assertEqual(cm.exception.messages[0], msg)
414415
self.assertEqual(cm.exception.code, 'max_value')
416+
with self.assertRaisesMessage(exceptions.ValidationError, msg):
417+
validator(NumericRange(0, None)) # an unbound range
415418

416419
def test_min(self):
417420
validator = RangeMinValueValidator(5)
418421
validator(NumericRange(10, 15))
422+
msg = 'Ensure that this range is completely greater than or equal to 5.'
419423
with self.assertRaises(exceptions.ValidationError) as cm:
420424
validator(NumericRange(0, 10))
421-
self.assertEqual(cm.exception.messages[0], 'Ensure that this range is completely greater than or equal to 5.')
425+
self.assertEqual(cm.exception.messages[0], msg)
422426
self.assertEqual(cm.exception.code, 'min_value')
427+
with self.assertRaisesMessage(exceptions.ValidationError, msg):
428+
validator(NumericRange(None, 10)) # an unbound range
423429

424430

425431
class TestFormField(PostgreSQLTestCase):

0 commit comments

Comments
 (0)