from django.core.validators import MaxLengthValidator as _MaxLengthValidator
from django.core.validators import MinLengthValidator as _MinLengthValidator
from django.utils.deconstruct import deconstructible
from django.utils.translation import ngettext_lazy
[docs]@deconstructible
class MinLengthValidator(_MinLengthValidator):
"""
Customer Django min length validator with better-suited error message
"""
code = "min_length"
message = ngettext_lazy(
"Ensure this value has at least %(limit_value)d items (it has %(show_value)d).",
"Ensure this value has at least %(limit_value)d items (it has %(show_value)d).",
"limit_value",
)
[docs] def compare(self, a, b):
return a < b
[docs] def clean(self, x):
return len(x)
[docs]@deconstructible
class MaxLengthValidator(_MaxLengthValidator):
"""
Customer Django max length validator with better-suited error message
"""
code = "max_length"
message = ngettext_lazy(
"Ensure this value has at most %(limit_value)d items (it has %(show_value)d).",
"Ensure this value has at most %(limit_value)d items (it has %(show_value)d).",
"limit_value",
)
[docs] def compare(self, a, b):
return a > b
[docs] def clean(self, x):
return len(x)