Skip to content

Commit eba403e

Browse files
committed
Add lenght validator
1 parent 6e4b0b5 commit eba403e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

django_mongodb_backend/fields/array.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def __init__(self, base_field, max_size=None, size=None, **kwargs):
4141
*self.default_validators,
4242
LengthValidator(self.size),
4343
]
44+
if self.fixed_size:
45+
self.default_validators = [
46+
*self.default_validators,
47+
LengthValidator(self.fixed_size),
48+
]
4449
# For performance, only add a from_db_value() method if the base field
4550
# implements it.
4651
if hasattr(self.base_field, "from_db_value"):
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from django.core.validators import BaseValidator
2+
from django.utils.deconstruct import deconstructible
3+
from django.utils.translation import ngettext_lazy
4+
5+
6+
@deconstructible
7+
class LengthValidator(BaseValidator):
8+
message = ngettext_lazy(
9+
"List contains %(show_value)d item, it should contain %(limit_value)d.",
10+
"List contains %(show_value)d items, it should contain %(limit_value)d.",
11+
"show_value",
12+
)
13+
code = "length"
14+
15+
def compare(self, a, b):
16+
return a == b
17+
18+
def clean(self, x):
19+
return len(x)

0 commit comments

Comments
 (0)