-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add validate method back to ListSerializer #2225
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -462,6 +462,42 @@ def get_value(self, dictionary): | |
return html.parse_html_list(dictionary, prefix=self.field_name) | ||
return dictionary.get(self.field_name, empty) | ||
|
||
def run_validation(self, data=empty): | ||
data = super(ListSerializer, self).run_validation(data) | ||
|
||
try: | ||
data = self.validate(data) | ||
assert data is not None, '.validate() should return the validated data' | ||
except ValidationError as exc: | ||
if isinstance(exc.detail, dict): | ||
# .validate() errors may be a dict, in which case, use | ||
# standard {key: list of values} style. | ||
raise ValidationError(dict([ | ||
(key, value if isinstance(value, list) else [value]) | ||
for key, value in exc.detail.items() | ||
])) | ||
elif isinstance(exc.detail, list): | ||
raise ValidationError({ | ||
api_settings.NON_FIELD_ERRORS_KEY: exc.detail | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use |
||
}) | ||
else: | ||
raise ValidationError({ | ||
api_settings.NON_FIELD_ERRORS_KEY: [exc.detail] | ||
}) | ||
except DjangoValidationError as exc: | ||
# Normally you should raise `serializers.ValidationError` | ||
# inside your codebase, but we handle Django's validation | ||
# exception class as well for simpler compat. | ||
# Eg. Calling Model.clean() explicitly inside Serializer.validate() | ||
raise ValidationError({ | ||
api_settings.NON_FIELD_ERRORS_KEY: list(exc.messages) | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a large copy/paste from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we dropped the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could also consider pushing this... https://github.com/tomchristie/django-rest-framework/blob/master/rest_framework/fields.py#L307-L320 into a |
||
|
||
return data | ||
|
||
def validate(self, attrs): | ||
return attrs | ||
|
||
def to_internal_value(self, data): | ||
""" | ||
List of dicts of native values <- List of dicts of primitive datatypes. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would probably recommend we drop the call to super here, in the same way we do for
Serializer
. It gets pretty confusing otherwise. This'd also let us move theif not isinstance(data, list)
check out ofto_internal_value
which would be nicer and would mirror theSerializer
classesif not isinstance(data, dict)
check that occurs.