Skip to content

Nested ListField serializer errors should include object reference #3274

@erikcw

Description

@erikcw

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy