-
-
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
Fix nested validation error being rendered incorrectly. #3801
Fix nested validation error being rendered incorrectly. #3801
Conversation
Previously an extra list wrapped nested validation errors raised from serializer's validate() methods. That was inconsistent with the format of validation errors raised by validate_<fieldname> methods. i.e. these two resulted in *different* behaviour: def validate_foo(self): raise ValidationError(['bar']) def validate(self): raise ValidationError({'foo': ['bar']})
Looks good to me. For clarity, could you show me:
|
I went all-out and tried three different approaches to raising the same error. In the process I found my description of the problem was slightly incorrect. The issue occurs when raising a nested This code shows all three cases: from rest_framework import serializers
# validation error is created by the outer serializer's validate() method
class Nested1(serializers.Serializer):
foo = serializers.CharField()
class Outer1(serializers.Serializer):
nested = Nested1()
def validate(self, attrs):
raise serializers.ValidationError({'nested': {'foo': ['bar']}})
# error is created by the inner serializer's validate() method
class Nested2(serializers.Serializer):
foo = serializers.CharField()
def validate(self, attrs):
raise serializers.ValidationError({'foo': ['bar']})
class Outer2(serializers.Serializer):
nested = Nested2()
# error is created by the inner serializer's validate_foo() method
class Nested3(serializers.Serializer):
foo = serializers.CharField()
def validate_foo(self, value):
raise serializers.ValidationError(['bar'])
class Outer3(serializers.Serializer):
nested = Nested3()
o1 = Outer1(data={'nested': {'foo': 'thing'}})
o1.is_valid()
o2 = Outer2(data={'nested': {'foo': 'thing'}})
o2.is_valid()
o3 = Outer3(data={'nested': {'foo': 'thing'}})
o3.is_valid()
print o1.errors
print o2.errors
print o3.errors This results in: {'nested': [{'foo': ['bar']}]}
{'nested': {'foo': ['bar']}}
{'nested': OrderedDict([('foo', ['bar'])])} Case 1 is the weird one; cases 2 and 3 are the same as each other (OrderedDict notwithstanding) My fix changes case 1, so the new output is: {'nested': {'foo': ['bar']}}
{'nested': {'foo': ['bar']}}
{'nested': OrderedDict([('foo', ['bar'])])} in which all three are equivalent. |
In light of what I've been doing for the error codes on validation errors in #3716, this makes full sense and the test case will help too 👍 |
Thanks - nice work! 😄 |
Fix nested validation error being rendered incorrectly.
Is it possible to avoid it for
|
Previously an extra list wrapped nested validation errors raised from serializer's
validate()
methods.That was inconsistent with the format of validation errors raised by
validate_<fieldname>
methods.i.e. these two resulted in different behaviour:
...