-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Description
I'm using REST framework to implement an API that's very verbose about errors in the request. One thing I cannot do right at this moment is reporting the type of a validation error. I need this type because automatically generated bindings make good use of it.
For example, when field is required, I need not only to say which field it is, but also add error code "missing". Other error codes include "blank", "incorrect_value" and so on.
I'm aiming for things like this in my error response:
{
"field": "/customer/address/city",
"code": "blank",
"details": "The attribute '/customer/address/city' can't be blank (neither null nor empty)."
}
Right now serializers.ValidationError
gives me everything I need, except the code. The type of a problem that a validator/field recognized is lost. We'll technically, it is described in the message carried by the exception, but that's not intended for such use.
By reading the source code, I've found method Field.fail(self, key, **kwargs)
. The key
is kinda what would do the trick for me if only it was available somewhere in the exception.
Any hints how I can solve my problem? Maybe it makes sense to turn this into an enhancement?
Right now I can monkey patch the Field.fail
, add the key
into exceptions and map known keys into my codes in an exception handler, but that's obviously an abuse of private implementation and will likely break when implementation changes.