Sibyx / django_api_forms

Declarative Django request validation for RESTful APIs
https://sibyx.github.io/django_api_forms/
MIT License
23 stars 13 forks source link

Linear validation errors #35

Closed Sibyx closed 2 years ago

Sibyx commented 2 years ago

This issue has been inspired by Problem Details for HTTP APIs - RFC7807 and blog post Structuring validation errors in REST APIs written by @k3nn7.

The main idea is to simplify the validation process on the client-side by flattening errors output. To achieve such a goal, the whole validation process have be rewritten (and luckily for us, much simplified). This will be a breaking change and will be released as 1.0.

The DetailedValidationError should be implemented which inherits from ValidationError and can keep position in lists nested forms. The form will return a linear list of errors instead of a nested dictionary.

Example request

{
  "authors": [
    {
      "name": "Bla",
      "surname": "Bla"
    },
    {
      "surname": "Hihi"
    }
  ],
  "password": "password"
}

Example validation error structure

{
  "errors": [
    {
      "path": ["authors", 1, "name"],
      "json-path": "$.authors.1.name",
      "message": "Field is required!",
      "code": "required"
    },
    {
      "path": ["password"],
      "json-path": "$.password",
      "message": "Password is too short!",
      "code": "too-short",
      "min-lenght": 10
    },
    {
      "path": ["password"],
      "json-path": "$.password",
      "message": "Password is too simple!",
      "code": "too-simple"
    },
    {
      "path": ["username"],
      "json_path": "$.username",
      "message": "Field have to be unique!",
      "code": "unique"
    }
  ]
}
Serbel97 commented 2 years ago

Can you assign me?