Skip to content

Commit 62dccca

Browse files
authored
feat: enforce Decimal type in min_value and max_value arguments of DecimalField
1 parent e08e606 commit 62dccca

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

rest_framework/fields.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,11 @@ def __init__(self, max_digits, decimal_places, coerce_to_string=None, max_value=
990990
self.max_value = max_value
991991
self.min_value = min_value
992992

993+
if self.max_value is not None and not isinstance(self.max_value, decimal.Decimal):
994+
raise ValueError("Invalid type of max_value argument. It must be decimal.")
995+
if self.min_value is not None and not isinstance(self.min_value, decimal.Decimal):
996+
raise ValueError("Invalid type of min_value argument. It must be decimal.")
997+
993998
if self.max_digits is not None and self.decimal_places is not None:
994999
self.max_whole_digits = self.max_digits - self.decimal_places
9951000
else:

tests/importable/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
This test "app" exists to ensure that parts of Django REST Framework can be
33
imported/invoked before Django itself has been fully initialized.
44
"""
5+
from decimal import Decimal
56

67
from rest_framework import compat, serializers # noqa
78

@@ -11,6 +12,6 @@ class ExampleSerializer(serializers.Serializer):
1112
charfield = serializers.CharField(min_length=1, max_length=2)
1213
integerfield = serializers.IntegerField(min_value=1, max_value=2)
1314
floatfield = serializers.FloatField(min_value=1, max_value=2)
14-
decimalfield = serializers.DecimalField(max_digits=10, decimal_places=1, min_value=1, max_value=2)
15+
decimalfield = serializers.DecimalField(max_digits=10, decimal_places=1, min_value=Decimal('1'), max_value=Decimal('2'))
1516
durationfield = serializers.DurationField(min_value=1, max_value=2)
1617
listfield = serializers.ListField(min_length=1, max_length=2)

tests/test_fields.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1203,17 +1203,17 @@ class TestMinMaxDecimalField(FieldValues):
12031203
Valid and invalid values for `DecimalField` with min and max limits.
12041204
"""
12051205
valid_inputs = {
1206-
'10.0': Decimal('10.0'),
1207-
'20.0': Decimal('20.0'),
1206+
'10.1': Decimal('10.1'),
1207+
'20.1': Decimal('20.1'),
12081208
}
12091209
invalid_inputs = {
1210-
'9.9': ['Ensure this value is greater than or equal to 10.'],
1211-
'20.1': ['Ensure this value is less than or equal to 20.'],
1210+
'9.9': ['Ensure this value is greater than or equal to 10.1.'],
1211+
'20.2': ['Ensure this value is less than or equal to 20.1.'],
12121212
}
12131213
outputs = {}
12141214
field = serializers.DecimalField(
12151215
max_digits=3, decimal_places=1,
1216-
min_value=10, max_value=20
1216+
min_value=Decimal('10.1'), max_value=Decimal('20.1')
12171217
)
12181218

12191219

@@ -1238,12 +1238,12 @@ class TestAllowEmptyStrDecimalFieldWithValidators(FieldValues):
12381238
outputs = {
12391239
None: '',
12401240
}
1241-
field = serializers.DecimalField(max_digits=3, decimal_places=1, allow_null=True, min_value=0, max_value=10)
1241+
field = serializers.DecimalField(max_digits=3, decimal_places=1, allow_null=True, min_value=Decimal('0'), max_value=Decimal('10'))
12421242

12431243

12441244
class TestNoMaxDigitsDecimalField(FieldValues):
12451245
field = serializers.DecimalField(
1246-
max_value=100, min_value=0,
1246+
max_value=Decimal('100'), min_value=Decimal('0'),
12471247
decimal_places=2, max_digits=None
12481248
)
12491249
valid_inputs = {

tests/test_model_serializer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,7 +1095,7 @@ class DecimalFieldModel(models.Model):
10951095
decimal_field = models.DecimalField(
10961096
max_digits=3,
10971097
decimal_places=1,
1098-
validators=[MinValueValidator(1), MaxValueValidator(3)]
1098+
validators=[MinValueValidator(decimal.Decimal('1')), MaxValueValidator(decimal.Decimal('3'))]
10991099
)
11001100

11011101

0 commit comments

Comments
 (0)