Skip to content

Commit 0b40fbc

Browse files
committed
Move length validator
1 parent ef67489 commit 0b40fbc

File tree

3 files changed

+20
-23
lines changed

3 files changed

+20
-23
lines changed

django_mongodb_backend/fields/array.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import json
22

3-
from django.contrib.postgres.validators import ArrayMaxLengthValidator
43
from django.core import checks, exceptions
54
from django.db.models import DecimalField, Field, Func, IntegerField, Transform, Value
65
from django.db.models.fields.mixins import CheckFieldDefaultMixin
@@ -10,7 +9,7 @@
109
from ..forms import SimpleArrayField
1110
from ..query_utils import process_lhs, process_rhs
1211
from ..utils import prefix_validation_error
13-
from .validators import LengthValidator
12+
from ..validators import ArrayMaxLengthValidator, LengthValidator
1413

1514
__all__ = ["ArrayField"]
1615

@@ -42,7 +41,6 @@ def __init__(self, base_field, max_size=None, size=None, **kwargs):
4241
if self.size:
4342
self.default_validators = [
4443
*self.default_validators,
45-
ArrayMaxLengthValidator(self.size),
4644
LengthValidator(self.size),
4745
]
4846
# For performance, only add a from_db_value() method if the base field
@@ -223,6 +221,7 @@ def formfield(self, **kwargs):
223221
"form_class": SimpleArrayField,
224222
"base_field": self.base_field.formfield(),
225223
"max_length": self.max_size,
224+
"size": self.size,
226225
**kwargs,
227226
}
228227
)

django_mongodb_backend/fields/validators.py

Lines changed: 0 additions & 19 deletions
This file was deleted.

django_mongodb_backend/validators.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from django.core.validators import MaxLengthValidator, MinLengthValidator
1+
from django.core.validators import BaseValidator, MaxLengthValidator, MinLengthValidator
2+
from django.utils.deconstruct import deconstructible
23
from django.utils.translation import ngettext_lazy
34

45

@@ -16,3 +17,19 @@ class ArrayMinLengthValidator(MinLengthValidator):
1617
"List contains %(show_value)d items, it should contain no fewer than %(limit_value)d.",
1718
"show_value",
1819
)
20+
21+
22+
@deconstructible
23+
class LengthValidator(BaseValidator):
24+
message = ngettext_lazy(
25+
"List contains %(show_value)d item, it should contain %(limit_value)d.",
26+
"List contains %(show_value)d items, it should contain %(limit_value)d.",
27+
"show_value",
28+
)
29+
code = "length"
30+
31+
def compare(self, a, b):
32+
return a != b
33+
34+
def clean(self, x):
35+
return len(x)

0 commit comments

Comments
 (0)