Skip to content

Classify validation errors #3111

@qsorix

Description

@qsorix

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy