mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-07-28 17:09:59 +03:00
fixed incorrect min/max attributes for serializers.ListField
This commit is contained in:
parent
a142467586
commit
329bd57fd0
|
@ -378,7 +378,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, field.validators, schema)
|
||||
|
||||
properties[field.field_name] = schema
|
||||
|
||||
|
@ -390,7 +390,7 @@ class AutoSchema(ViewInspector):
|
|||
|
||||
return result
|
||||
|
||||
def _map_field_validators(self, validators, schema):
|
||||
def _map_field_validators(self, field, validators, schema):
|
||||
"""
|
||||
map field validators
|
||||
:param list:validators: list of field validators
|
||||
|
@ -406,9 +406,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):
|
||||
|
|
|
@ -395,6 +395,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