[FEATURE] Overriding Validation Error default schema used in API document
See original GitHub issueIn my app, I customize RequestValidationError response using exception_handler
as below:
@app.exception_handler(RequestValidationError)
async def request_validation_exception_handler(_request: Request, exc: RequestValidationError) -> JSONResponse:
data = {
"error": {
"code": Error.REQUEST_VALIDATION_ERROR.value,
"message": "Intput data is invalid.",
"detail": exc.errors(),
}
}
return JSONResponse(content=data, status_code=HTTP_422_UNPROCESSABLE_ENTITY)
The generated API document includes automatically the default Validation Error schema, HTTPValidationError
, in all POST or PUT path operations.
So I need to add my responses definition one by one.
responses = {
422: {'model': APIErrorResponse, 'description': 'Validation Error'}
}
@router.post("/", responses={**responses})
async def create_user(user_in: UserCreate):
"""Create new user.
"""
@router.put("/", responses={**responses})
async def update_user(user_in: UserUpdate):
"""Update user.
"""
It would be useful if we are able to override the Validation Error default schema used in API document.
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (3 by maintainers)
Top Results From Across the Web
Azure API Management validation policies | Microsoft Learn
Reference for Azure API Management policies to validate API requests and responses. Provides policy usage, settings, and examples.
Read more >Validating OpenAPI and JSON Schema
In this article, we'll cover how to configure the default JSON Schema dialect of an OpenAPI 3.1 document and how to validate that...
Read more >Schema validator - OpenAPI4J
ADDITIONAL_PROPS_RESTRICT : By default, Schema Object can have additional properties. This option let's you invert the behaviour. You can override keywords and ...
Read more >Schema Registry API Reference | Confluent Documentation
BACKWARD : (default) consumers using the new schema can read data written by producers using the latest registered schema; BACKWARD_TRANSITIVE : consumers using ......
Read more >Validation-and-Serialization - Fastify
Treat the schema definition as application code. Validation and serialization features dynamically evaluate code with new Function() , which is not safe to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
@tiangolo Thanks a lot!
Using the
responses
parameter ininclude_router()
method, the API documentation now reflects the custom validation error schema.You can set the
responses
parameter at the router level: https://fastapi.tiangolo.com/tutorial/bigger-applications/