-
First Check
Commit to Help
Example Code@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request, exc):
errors = exc.errors()
custom_error = []
for error in errors:
custom_error.append(error['msg'])
return JSONResponse(status_code=422, content=jsonable_encoder({'detail': custom_error}))DescriptionAfter I custom the RequestValidationError above, the swagger's schema is still the original one. Operating SystemWindows Operating System DetailsNo response FastAPI Version0.79.0 Python Version3.9.13 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments
-
|
Not sure if adding it this way changes OpenAPI doc. |
Beta Was this translation helpful? Give feedback.
-
|
@iudeen is correct, adding a handler does not automagically add that to the generated validation_error_definition = {
"title": "ValidationError",
"type": "object",
"properties": {
"loc": {
"title": "Location",
"type": "array",
"items": {"anyOf": [{"type": "string"}, {"type": "integer"}]},
},
"msg": {"title": "Message", "type": "string"},
"type": {"title": "Error Type", "type": "string"},
},
"required": ["loc", "msg", "type"],
}
validation_error_response_definition = {
"title": "HTTPValidationError",
"type": "object",
"properties": {
"detail": {
"title": "Detail",
"type": "array",
"items": {"$ref": REF_PREFIX + "ValidationError"},
}
},
} |
Beta Was this translation helpful? Give feedback.
-
|
@JarroVGIT Thanks, but how could I add them to endpoints? |
Beta Was this translation helpful? Give feedback.
-
|
It’s described here in the docs . You can add a 422 response in the |
Beta Was this translation helpful? Give feedback.
-
|
@JarroVGIT Thanks again! I'll close this issue~ |
Beta Was this translation helpful? Give feedback.
It’s described here in the docs . You can add a 422 response in the
responsesparameter of the decorator and that will override the default.