-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Closed
Labels
Description
When validation errors happen in nested ListField serializers -- an OrderedDict is returned for the ListField containing the nested errors. However, it doesn't indicate which object in the list caused the error.
For example, my nested serializer has a URLField, if I pass in a list of 10 objects -- the error doesn't indicate which of the 10 objects contains the error.
class ChildSerializer(serializers.Serializer):
url = serializers.URLField()
uuid = serializers.UUIDField()
other_fields = serializers.CharField() # many more...
class MySerializer(serializers.ModelSerializer):
# my_nested_list_field is a JSONfield on the Django model
my_nested_list_field = serializer.ListField(child=ChildSerializer())
data = {#other fields...,
'my_nested_listfield': [
{"url": "some invalid url", "other_fields": "MANY"},
{"url": "http://www.google.com/", "other_fields": "MANY"},
#9 (n) more dicts...
]
}
x = MySerializer(data=data)
x.is_valid()
x.errors
{'my_nested_listfield': OrderedDict([('url', [u'Enter a valid URL.'])])}
# did this error occur in dict #1, #5, #10???? Start trawling through the list looking for the bad URL...
Nested errors should probably contain some metadata to indicate which item(s) failed validation so that useful errors can be displayed to the client.
Something like:
{'my_nested_listfield': OrderedDict([('url', [u'Enter a valid URL.'] "_meta_ref": "OBJECT_123")])}
# Oh, I see, the error occured on OBJECT_123 -- this will be quick and easy to debug
Where _meta_ref
is generated by an overrideable method on the child serializer. It could default to the object url
-- but in this case I would use an internal id that is meaningful to my application, like the uuid