You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Django REST Framework recommends raising a ValidationError in to_internal_value() method of a custom field to report a validation error (see DRF > API Guide > Fields > Raising validation errors). The plugin crashes with AttributeError when I do that. It assumes that the serializer has a method called validate_<field name> while it does not.
How to reproduce
Here's a custom field example taken from DRF docs (linked above). Imports and serializers added by me:
# color_serializers.pyimportreimportsixfromrest_framework.serializersimportField, Serializer, ValidationErrorfromrest_framework_friendly_errors.mixinsimportFriendlyErrorMessagesMixinclassColor(object):
""" A color represented in the RGB colorspace. """def__init__(self, red, green, blue):
assert(red>=0andgreen>=0andblue>=0)
assert(red<256andgreen<256andblue<256)
self.red, self.green, self.blue=red, green, blueclassColorField(Field):
""" Color objects are serialized into 'rgb(#, #, #)' notation. """defto_representation(self, obj):
return"rgb(%d, %d, %d)"% (obj.red, obj.green, obj.blue)
defto_internal_value(self, data):
ifnotisinstance(data, six.text_type):
msg='Incorrect type. Expected a string, but got %s'raiseValidationError(msg%type(data).__name__)
ifnotre.match(r'^rgb\([0-9]+,[0-9]+,[0-9]+\)$', data):
raiseValidationError('Incorrect format. Expected `rgb(#,#,#)`.')
data=data.strip('rgb(').rstrip(')')
red, green, blue= [int(col) forcolindata.split(',')]
ifany([col>255orcol<0forcolin (red, green, blue)]):
raiseValidationError('Value out of range. Must be between 0 and 255.')
returnColor(red, green, blue)
classColorSerializer(Serializer):
color=ColorField()
classFriendlyColorSerializer(FriendlyErrorMessagesMixin, Serializer):
color=ColorField()
Normal serializer works as expected
from .color_serializersimportColorSerializercolor_serializer=ColorSerializer(data= {'color': u'not a color'})
color_serializer.is_valid(raise_exception=True)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework/serializers.py", line 222, in is_valid
raise ValidationError(self.errors)
ValidationError: {'color': [u'Incorrect format. Expected `rgb(#,#,#)`.']}
Serializer with FriendlyErrorMessagesMixin raises AttributeError
from .color_serializersimportFriendlyColorSerializerfriendly_color_serializer=FriendlyColorSerializer(data= {'color': u'not a color'})
friendly_color_serializer.is_valid(raise_exception=True)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework/serializers.py", line 222, in is_valid
raise ValidationError(self.errors)
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 23, in errors
pretty_errors = self.build_pretty_errors(ugly_errors)
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 204, in build_pretty_errors
self.get_field_error_entries(errors[error_type], field),
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 178, in get_field_error_entries
return [self.get_field_error_entry(error, field) for error in errors]
File "/home/cameel/.virtualenvs/test/lib/python2.7/site-packages/rest_framework_friendly_errors/mixins.py", line 166, in get_field_error_entry
validator = getattr(self, "validate_%s" % field.field_name)
AttributeError: 'FriendlyColorSerializer' object has no attribute 'validate_color'
The text was updated successfully, but these errors were encountered:
Thanks for reporting it. We'll look into this but I can't give any estimates right now. Our core maintainer of this module is currently occupied with other tasks.
Feel free to send a PR with a fix proposal if you have one.
Django REST Framework recommends raising a
ValidationError
into_internal_value()
method of a custom field to report a validation error (see DRF > API Guide > Fields > Raising validation errors). The plugin crashes withAttributeError
when I do that. It assumes that the serializer has a method calledvalidate_<field name>
while it does not.How to reproduce
Here's a custom field example taken from DRF docs (linked above). Imports and serializers added by me:
Normal serializer works as expected
Serializer with FriendlyErrorMessagesMixin raises AttributeError
The text was updated successfully, but these errors were encountered: