Closed slavugan closed 2 years ago
Can you show me some code so I understand the issue and potential solution?
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.
so its two different issues.
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?
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.
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"
}
]
}
v1.3.6 released.
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.