[FEATURE] Allow customization of validation error
See original GitHub issueIs your feature request related to a problem
I’d like to globally customize validation errors by changing (overriding) the HTTPValidationError
/ValidationError
and the 422
status code.
The solution you would like
Something great would be adding some parameters to the FastAPI
class:
from fastapi import FastAPI
from .models import ValidationError
app = FastAPI(
validation_error_status_code=422,
validation_error_model=ValidationError
)
Describe alternatives you’ve considered
What’s currently possible (to my knowledge) is adding an item with status code 422
, 4XX
or default
in the responses
dict, but this as to be done manually for every route that will perform validation. This also prevent changing the status code to a specific value (you can either stick with 422, or have something vague like default
or 4XX
).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:15
- Comments:16 (1 by maintainers)
Top Results From Across the Web
In screen flow, allow custom validation error messages on ...
When a component is added to a screen and the 'Required' checkbox is checked, allow Admins to enter a custom error message to...
Read more >Providing custom error messages for built-in HTML5 form ...
How to customize built-in form validation error messages · Grab the input element(s) with a querySelector / querySelectorAll . · Add an event ......
Read more >Custom Validation Error Messages - Your own talent platform
This guide describes how an admin can create custom error messages to inform a user on the required inputs in a custom data...
Read more >Allow forms to set custom validation error messages ... - Drupal
Allow forms to set custom validation error messages on required fields · Problem/Motivation · Proposed resolution · Remaining tasks · User interface ...
Read more >About Validation Error Messages - Contentstack
If you do not specify any custom error message in this property (and assuming that you have specified certain rules in Validation (regex)...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
If all you errors follow the same schema, and provided you implemented the custom handler described in https://fastapi.tiangolo.com/tutorial/handling-errors/#override-request-validation-exceptions, you can just override the default schema in
openapi/utils
.For example if I take the example server from the docs:
it will present the default schema il http://localhost:8000/docs:
But if I just overwrite the
validation_error_response_definition
in thefastapi.openapi.utils
module it changes the default schema used by OpenAPI:gives:
As this is still the topic… For all of you, who, like me, just want to change the status code to 400, but keep the default response schema AND still have it in the OpenAPI Docs, without the default 422… 😄 Here it goes…
Firstly, you describe your Pydantic models, like the original (used only for docs):
Then you use Pydantic model
ErrorResponse
in responses when defining your app:Then you override the default exception handler (like in the tutorial):
With this, you will have 400 normally documented for all your routes.
If you want to remove all 422 codes from the documentation, you need to exchange the default OpenAPI schema, similar to how @tupui wrote above, or according to the tutorial here. Therefore, after defining all your path routes, add the following:
However, this works for the default API with no additional root paths. In order to make it work for such scenario, or when you want this to work only on the mounted API, you have to add
servers
to theopenapi_schema
as well, to have the proper routing in Swagger/Redoc. For example:P.S. If you really wanna be persistent, and remove even the
ValidationError
andHTTPValidationError
from schemas, you can add the following to yourcustom_openapi()
method:Hope this helps! 😄
Thanks again @tiangolo for all the awesome work on the framework! 🚀