[Django] #36019: MinValueValidator and MaxValueValidator messages do not match compare operators

9 views
Skip to first unread message

Django

unread,
Dec 17, 2024, 5:33:35 AM12/17/24
to django-...@googlegroups.com
#36019: MinValueValidator and MaxValueValidator messages do not match compare
operators
-------------------------------------+-------------------------------------
Reporter: Samuel Nussbaumer | Type: Bug
Status: new | Component: Core
| (Other)
Version: 5.1 | Severity: Normal
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 1
-------------------------------------+-------------------------------------
Excerpt from
[https://github.com/django/django/blob/main/django/core/validators.py
/django/core/validators.py]:

{{{
@deconstructible
class MaxValueValidator(BaseValidator):
message = _("Ensure this value is **less than or equal** to
%(limit_value)s.")
code = "max_value"

def compare(self, a, b):
return a > b


@deconstructible
class MinValueValidator(BaseValidator):
message = _("Ensure this value is **greater than or equal** to
%(limit_value)s.")
code = "min_value"

def compare(self, a, b):
return a < b
}}}

The messages imply that the comparison between a and b would be a <= b or
a >= b whereas in practice they only check if a is greater or lesser than
b.

Reprod:
1. Define a form field with validators=[MaxValueValidator(3),
MinValueValidator(0.2)]
2. On the form, type in 0.2 and save -> ValidationError: Ensure this value
is greater than or equal to 0.2.
Expected behavior: The form should not raise any ValidationErrors
(according to the error message)
--
Ticket URL: <https://code.djangoproject.com/ticket/36019>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Dec 17, 2024, 5:49:13 AM12/17/24
to django-...@googlegroups.com
#36019: MinValueValidator and MaxValueValidator messages do not match compare
operators
-----------------------------------+--------------------------------------
Reporter: Samuel Nussbaumer | Owner: (none)
Type: Bug | Status: new
Component: Core (Other) | Version: 5.1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 1
-----------------------------------+--------------------------------------
Changes (by Samuel Nussbaumer):

* cc: Samuel Nussbaumer (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/36019#comment:1>

Django

unread,
Dec 17, 2024, 7:10:00 AM12/17/24
to django-...@googlegroups.com
#36019: MinValueValidator and MaxValueValidator messages do not match compare
operators
-----------------------------------+--------------------------------------
Reporter: Samuel Nussbaumer | Owner: (none)
Type: Bug | Status: closed
Component: Core (Other) | Version: 5.1
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 1
-----------------------------------+--------------------------------------
Changes (by Sarah Boyce):

* resolution: => duplicate
* status: new => closed

Comment:

This appears to be a duplicate of #32037

The logic is correct. I guess this is what is causing confusion
{{{
>>> from decimal import Decimal
>>> Decimal("0.2") < 0.2
True
}}}
And so...
{{{
>>> from django.core.validators import MaxValueValidator,
MinValueValidator
>>> from django import forms
>>> field = forms.DecimalField(validators=[MaxValueValidator(3.0),
MinValueValidator(Decimal("0.2"))])
>>> field.clean(Decimal("0.2"))
Decimal('0.2')
>>> field = forms.DecimalField(validators=[MaxValueValidator(3.0),
MinValueValidator(0.2)])
>>> field.clean(Decimal("0.2"))
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/path_to_venv/lib/python3.10/site-
packages/django/forms/fields.py", line 206, in clean
self.run_validators(value)
File "/path_to_venv/site-packages/django/forms/fields.py", line 197, in
run_validators
raise ValidationError(errors)
django.core.exceptions.ValidationError: ['Ensure this value is greater
than or equal to 0.2.']
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36019#comment:2>
Reply all
Reply to author
Forward
0 new messages