mirror of
https://github.com/django/django.git
synced 2025-05-05 06:27:31 +00:00
Fixed #28758 -- Fixed RangeMax/MinValueValidators crash with unbound ranges.
This commit is contained in:
parent
1b7780ea08
commit
3e7497a05e
@ -69,11 +69,11 @@ class KeysValidator:
|
|||||||
|
|
||||||
class RangeMaxValueValidator(MaxValueValidator):
|
class RangeMaxValueValidator(MaxValueValidator):
|
||||||
def compare(self, a, b):
|
def compare(self, a, b):
|
||||||
return a.upper > b
|
return a.upper is None or a.upper > b
|
||||||
message = _('Ensure that this range is completely less than or equal to %(limit_value)s.')
|
message = _('Ensure that this range is completely less than or equal to %(limit_value)s.')
|
||||||
|
|
||||||
|
|
||||||
class RangeMinValueValidator(MinValueValidator):
|
class RangeMinValueValidator(MinValueValidator):
|
||||||
def compare(self, a, b):
|
def compare(self, a, b):
|
||||||
return a.lower < b
|
return a.lower is None or a.lower < b
|
||||||
message = _('Ensure that this range is completely greater than or equal to %(limit_value)s.')
|
message = _('Ensure that this range is completely greater than or equal to %(limit_value)s.')
|
||||||
|
@ -408,18 +408,24 @@ class TestValidators(PostgreSQLTestCase):
|
|||||||
def test_max(self):
|
def test_max(self):
|
||||||
validator = RangeMaxValueValidator(5)
|
validator = RangeMaxValueValidator(5)
|
||||||
validator(NumericRange(0, 5))
|
validator(NumericRange(0, 5))
|
||||||
|
msg = 'Ensure that this range is completely less than or equal to 5.'
|
||||||
with self.assertRaises(exceptions.ValidationError) as cm:
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
||||||
validator(NumericRange(0, 10))
|
validator(NumericRange(0, 10))
|
||||||
self.assertEqual(cm.exception.messages[0], 'Ensure that this range is completely less than or equal to 5.')
|
self.assertEqual(cm.exception.messages[0], msg)
|
||||||
self.assertEqual(cm.exception.code, 'max_value')
|
self.assertEqual(cm.exception.code, 'max_value')
|
||||||
|
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
||||||
|
validator(NumericRange(0, None)) # an unbound range
|
||||||
|
|
||||||
def test_min(self):
|
def test_min(self):
|
||||||
validator = RangeMinValueValidator(5)
|
validator = RangeMinValueValidator(5)
|
||||||
validator(NumericRange(10, 15))
|
validator(NumericRange(10, 15))
|
||||||
|
msg = 'Ensure that this range is completely greater than or equal to 5.'
|
||||||
with self.assertRaises(exceptions.ValidationError) as cm:
|
with self.assertRaises(exceptions.ValidationError) as cm:
|
||||||
validator(NumericRange(0, 10))
|
validator(NumericRange(0, 10))
|
||||||
self.assertEqual(cm.exception.messages[0], 'Ensure that this range is completely greater than or equal to 5.')
|
self.assertEqual(cm.exception.messages[0], msg)
|
||||||
self.assertEqual(cm.exception.code, 'min_value')
|
self.assertEqual(cm.exception.code, 'min_value')
|
||||||
|
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
||||||
|
validator(NumericRange(None, 10)) # an unbound range
|
||||||
|
|
||||||
|
|
||||||
class TestFormField(PostgreSQLTestCase):
|
class TestFormField(PostgreSQLTestCase):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user