summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbuzzi <buzzi.javier@gmail.com>2018-10-17 14:52:19 +0000
committerTim Graham <timograham@gmail.com>2018-10-22 10:26:54 -0400
commit24cae0bedc51093b363c323af555946a8edea1a1 (patch)
tree39afc03c2f0ee34844ae383d1044e89e2607c726
parent5e3463f6bcec818431f0e1f4649d6a5bd944c459 (diff)
Fixed #29860 -- Allowed BaseValidator to accept a callable limit_value.
-rw-r--r--django/core/validators.py5
-rw-r--r--docs/ref/validators.txt28
-rw-r--r--docs/releases/2.2.txt4
-rw-r--r--tests/validators/tests.py4
4 files changed, 34 insertions, 7 deletions
diff --git a/django/core/validators.py b/django/core/validators.py
index c1c9cd1c87..38e4b6aa1d 100644
--- a/django/core/validators.py
+++ b/django/core/validators.py
@@ -317,8 +317,9 @@ class BaseValidator:
def __call__(self, value):
cleaned = self.clean(value)
- params = {'limit_value': self.limit_value, 'show_value': cleaned, 'value': value}
- if self.compare(cleaned, self.limit_value):
+ limit_value = self.limit_value() if callable(self.limit_value) else self.limit_value
+ params = {'limit_value': limit_value, 'show_value': cleaned, 'value': value}
+ if self.compare(cleaned, limit_value):
raise ValidationError(self.message, code=self.code, params=params)
def __eq__(self, other):
diff --git a/docs/ref/validators.txt b/docs/ref/validators.txt
index 6294d519f8..b6a233014d 100644
--- a/docs/ref/validators.txt
+++ b/docs/ref/validators.txt
@@ -236,7 +236,12 @@ to, or in lieu of custom ``field.clean()`` methods.
.. class:: MaxValueValidator(limit_value, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
- ``'max_value'`` if ``value`` is greater than ``limit_value``.
+ ``'max_value'`` if ``value`` is greater than ``limit_value``, which may be
+ a callable.
+
+ .. versionchanged:: 2.2
+
+ ``limit_value`` can now be a callable.
``MinValueValidator``
---------------------
@@ -244,7 +249,12 @@ to, or in lieu of custom ``field.clean()`` methods.
.. class:: MinValueValidator(limit_value, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
- ``'min_value'`` if ``value`` is less than ``limit_value``.
+ ``'min_value'`` if ``value`` is less than ``limit_value``, which may be a
+ callable.
+
+ .. versionchanged:: 2.2
+
+ ``limit_value`` can now be a callable.
``MaxLengthValidator``
----------------------
@@ -252,7 +262,12 @@ to, or in lieu of custom ``field.clean()`` methods.
.. class:: MaxLengthValidator(limit_value, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
- ``'max_length'`` if the length of ``value`` is greater than ``limit_value``.
+ ``'max_length'`` if the length of ``value`` is greater than
+ ``limit_value``, which may be a callable.
+
+ .. versionchanged:: 2.2
+
+ ``limit_value`` can now be a callable.
``MinLengthValidator``
----------------------
@@ -260,7 +275,12 @@ to, or in lieu of custom ``field.clean()`` methods.
.. class:: MinLengthValidator(limit_value, message=None)
Raises a :exc:`~django.core.exceptions.ValidationError` with a code of
- ``'min_length'`` if the length of ``value`` is less than ``limit_value``.
+ ``'min_length'`` if the length of ``value`` is less than ``limit_value``,
+ which may be a callable.
+
+ .. versionchanged:: 2.2
+
+ ``limit_value`` can now be a callable.
``DecimalValidator``
--------------------
diff --git a/docs/releases/2.2.txt b/docs/releases/2.2.txt
index 90099d9fc3..4a6e74fab3 100644
--- a/docs/releases/2.2.txt
+++ b/docs/releases/2.2.txt
@@ -259,7 +259,9 @@ URLs
Validators
~~~~~~~~~~
-* ...
+* :class:`.MaxValueValidator`, :class:`.MinValueValidator`,
+ :class:`.MinLengthValidator`, and :class:`.MaxLengthValidator` now accept
+ a callable ``limit_value``.
.. _backwards-incompatible-2.2:
diff --git a/tests/validators/tests.py b/tests/validators/tests.py
index 9f69854902..36d0b2a520 100644
--- a/tests/validators/tests.py
+++ b/tests/validators/tests.py
@@ -203,6 +203,10 @@ TEST_DATA = [
(MinValueValidator(0), -1, ValidationError),
(MinValueValidator(NOW), NOW - timedelta(days=1), ValidationError),
+ # limit_value may be a callable.
+ (MinValueValidator(lambda: 1), 0, ValidationError),
+ (MinValueValidator(lambda: 1), 1, None),
+
(MaxLengthValidator(10), '', None),
(MaxLengthValidator(10), 10 * 'x', None),