mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-10 19:56:59 +03:00
Fixed min/max attributes for serializers.ListField (#6866)
This commit is contained in:
parent
f8c16441fa
commit
1cc4be47b4
|
@ -377,7 +377,7 @@ class AutoSchema(ViewInspector):
|
|||
schema['default'] = field.default
|
||||
if field.help_text:
|
||||
schema['description'] = str(field.help_text)
|
||||
self._map_field_validators(field.validators, schema)
|
||||
self._map_field_validators(field, schema)
|
||||
|
||||
properties[field.field_name] = schema
|
||||
|
||||
|
@ -389,13 +389,11 @@ class AutoSchema(ViewInspector):
|
|||
|
||||
return result
|
||||
|
||||
def _map_field_validators(self, validators, schema):
|
||||
def _map_field_validators(self, field, schema):
|
||||
"""
|
||||
map field validators
|
||||
:param list:validators: list of field validators
|
||||
:param dict:schema: schema that the validators get added to
|
||||
"""
|
||||
for v in validators:
|
||||
for v in field.validators:
|
||||
# "Formats such as "email", "uuid", and so on, MAY be used even though undefined by this specification."
|
||||
# https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#data-types
|
||||
if isinstance(v, EmailValidator):
|
||||
|
@ -405,9 +403,15 @@ class AutoSchema(ViewInspector):
|
|||
if isinstance(v, RegexValidator):
|
||||
schema['pattern'] = v.regex.pattern
|
||||
elif isinstance(v, MaxLengthValidator):
|
||||
schema['maxLength'] = v.limit_value
|
||||
attr_name = 'maxLength'
|
||||
if isinstance(field, serializers.ListField):
|
||||
attr_name = 'maxItems'
|
||||
schema[attr_name] = v.limit_value
|
||||
elif isinstance(v, MinLengthValidator):
|
||||
schema['minLength'] = v.limit_value
|
||||
attr_name = 'minLength'
|
||||
if isinstance(field, serializers.ListField):
|
||||
attr_name = 'minItems'
|
||||
schema[attr_name] = v.limit_value
|
||||
elif isinstance(v, MaxValueValidator):
|
||||
schema['maximum'] = v.limit_value
|
||||
elif isinstance(v, MinValueValidator):
|
||||
|
|
|
@ -459,6 +459,9 @@ class TestOperationIntrospection(TestCase):
|
|||
assert properties['string']['minLength'] == 2
|
||||
assert properties['string']['maxLength'] == 10
|
||||
|
||||
assert properties['lst']['minItems'] == 2
|
||||
assert properties['lst']['maxItems'] == 10
|
||||
|
||||
assert properties['regex']['pattern'] == r'[ABC]12{3}'
|
||||
assert properties['regex']['description'] == 'must have an A, B, or C followed by 1222'
|
||||
|
||||
|
|
|
@ -85,6 +85,12 @@ class ExampleValidatedSerializer(serializers.Serializer):
|
|||
),
|
||||
help_text='must have an A, B, or C followed by 1222'
|
||||
)
|
||||
lst = serializers.ListField(
|
||||
validators=(
|
||||
MaxLengthValidator(limit_value=10),
|
||||
MinLengthValidator(limit_value=2),
|
||||
)
|
||||
)
|
||||
decimal1 = serializers.DecimalField(max_digits=6, decimal_places=2)
|
||||
decimal2 = serializers.DecimalField(max_digits=5, decimal_places=0,
|
||||
validators=(DecimalValidator(max_digits=17, decimal_places=4),))
|
||||
|
|
Loading…
Reference in New Issue
Block a user