PostHog / drf-exceptions-hog

Standardized and easy-to-parse API error responses for Django REST Framework (DRF).

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not handling Validation error for nested serializers

bedilbek opened this issue · comments

Hi, thanks for this repo.

I got one issue when working with a nested serializer.

Let's say I have a UserSerializer with nested foreign key extra_info

class UserSerializer(serializers.Serializer):
    class UserExtraInfoSerializer(serializers.Serializer):
        mobile_phone_number = serializers.CharField(max_length=15, required=True)

    first_name = serializers.CharField(max_length=100)
    extra_info = UserExtraInfoSerializer()

Then, let's assume client wants to send a request with the below POST data to the view handled by UserSerializer:

{"first_name": "Someone", "extra_info": {}}

The client should receive ValidationError, because when extra_info is present mobile_phone_number is required. But because it is not handled in drf-exceptions-hog. I guess I found the place where the fix is necessary:

key = next(iter(codes)) # Get first key
code = codes[key] if isinstance(codes[key], str) else codes[key][0]

Because we are handling nested serializer, we need to have one more condition to check if there is dict inside (the best even iterate over all dictionaries to find the root). I will find a workaround for my project, maybe even make PR, hopefully