litestar-org / litestar

Production-ready, Light, Flexible and Extensible ASGI API framework | Effortlessly Build Performant APIs
https://litestar.dev/
MIT License
5.64k stars 382 forks source link

Data validation error response is not usable on front end side #102

Closed slavugan closed 2 years ago

slavugan commented 2 years ago

For data validation in request handler parameters Starlite returns JSON detail: "Validation failed for POST http://localhost:8000/api/a/brand/:\n\ndata -> age\n field required (type=value_error.missing)" extra: null It is hard to use such response for form validation on front end side.

Besides validation errors breaks CORS middleware which makes response body unavailable for client app in browser. I think it would be more convenient instead raising Starlite ValidationException leave Pydantic ValidationError. It would make it possible to handle it on middleware level and fix CORS.

Goldziher commented 2 years ago

Can you show me some code so I understand the issue and potential solution?

slavugan commented 2 years ago

For example we have some pydantic model and model controller

class SomeModel(BaseModel):
    name: str
    value: int

class ModelController(Controller):
    path = '/model'

    @post()
    async def create(self, data: SomeModel) -> str:
        print('post data:', data)
        return 'ok'

For example "value" field is missing in request data, we get such response

{
    "detail":"Validation failed for POST http://localhost:8000/api/a/model:\n\ndata -> value\n  field required (type=value_error.missing)",
    "extra":null
}

And CORS header is absent in response.

Pydantic ValidationError provides nice json and validation data which can be used for error response.

Goldziher commented 2 years ago

so its two different issues.

  1. CORS headers.
  2. you are not happy with the json object being returned.

So, please create a different issue for the cors headers so we are able to track that one separately and lets focus here on the error object that is being sent. Can you elaborate what is the problemn with this error object and how you would like the error object to be?

slavugan commented 2 years ago

Response should contain field name or list of fields which not passed validation, error type to identify error on client side and some message for user to show. Json provided by Pydantic validation is pretty fine, but it would be good to have possibility to change that data, maybe add translations to error messages or something else.

Goldziher commented 2 years ago

Ok, so version 1.3.6 introduces an update to this. Validation errors will be returned with the following body:

{
   "detail":"Validation failed for POST http://testserver/123",
   "extra":[
      {
         "loc":[
            "data",
            "last_name"
         ],
         "msg":"field required",
         "type":"value_error.missing"
      },
      {
         "loc":[
            "data",
            "id"
         ],
         "msg":"field required",
         "type":"value_error.missing"
      },
      {
         "loc":[
            "data",
            "complex"
         ],
         "msg":"field required",
         "type":"value_error.missing"
      }
   ]
}
Goldziher commented 2 years ago

v1.3.6 released.