-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
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
ListField validation errors are inconsistent with ListSerializer #7279
Comments
How to replicate it ? i tried following class TestSerializer(serializers.Serializer):
foo = serializers.ListField(required=False)
class ObjectListSerializer(serializers.ListSerializer):
child = TestSerializer()
input_data = [
{},
{"foo": ["error"]},
{},
{"foo": ["another error"]}
]
serializer = ObjectListSerializer(data=input_data) |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
I agree with the original issue,
Does this help ? class NestedTestSerializer(serializers.Serializer):
nested_serializer_field = serializers.CharField()
class TestSerializer(serializers.Serializer):
list_serializer = NestedTestSerializer(many=True)
list_field = serializers.ListField(child=serializers.DictField(allow_empty=False))
def test_errors_inconsistencies():
serializer = TestSerializer(
data={
"list_serializer": [{"nested_serializer_field": ""}],
"list_field": [{}],
}
)
serializer.is_valid()
# list_serializer error is: [{"nested_serializer_field": ["This field may not be blank."]}]
assert isinstance(serializer.errors["list_serializer"], list)
# list_field error is: {"0": ["This dictionary may not be empty."]}
assert isinstance(serializer.errors["list_field"], dict) As you can see, both It is caused by It is debatable if the correct error type is a
|
I am also in the camp that returning a dict of errors is more likely to be useful when actually handling these errors and/or debugging, I also think it's more likely to be the behavior that developers are expecting. However in the event that consensus cannot be reached I would propose that the documentation be updated to make this difference clear between ListSerializer (which you get by default with the many=True flag) and ListField. Especially since passing a many=True flag is strongly encouraged whenever you are dealing with nested serializers. I'm sure there are other important differences as well but I haven't dug into them (maybe the create/update behaviour?) |
a document update and later updating or aligning the errors would be the go. any takers? |
I don't really have enough time, also I don't really agree with the style the documentation is written in so I would inevitably fall down a deep rabbit hole trying to update them. |
Validation errors when using ListSerializer are returned as a list with non-failing entries empty:
When using ListField the same information is unexpectedly returned as a dictionary:
The current ListField error handling was implemented in #5655 along with DictField, which explains this behavior but I think it would be more obvious for ListField to behave like ListSerializer and return a list instead.
The text was updated successfully, but these errors were encountered: