Hello,
Could anyone advise of the best approach for raising a ValidationError in the Django REST framework from a serializer’s to_internal_value() function?
I need to do a simple mapping from the input value (a string) to a boolean and I want to throw errors if the value coming in to the serializer either 1) isn’t a string or 2) is not one of a number of discrete values.
If I define my ValidationErrors like this in the serializer’s to_internal_value() function (as per Exceptions - Django REST framework):
ValidationError({field_name: error_message})
…and run a unit test to check that the error has been raised, the DRF unit test framework responds with:
AttributeError: 'ErrorDetail' object has no attribute 'items'
… when trying to iterate through the serializer’s errors.:
self.assertTrue(serializer.is_valid(), serializer.errors)
Somehow, the ErrorDetail class from DRF seems to have gotten involved.
Is there a specific way that I can add the error to the serializer’s existing list of errors?
Due to my project’s setup and complexity, I cannot produce a simple test case, I’m afraid.
Thanks for any advice.