question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] Overriding Validation Error default schema used in API document

See original GitHub issue

In 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:closed
  • Created 4 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
otsukacommented, Apr 13, 2020

@tiangolo Thanks a lot!

Using the responses parameter in include_router() method, the API documentation now reflects the custom validation error schema.

1reaction
tiangolocommented, Apr 12, 2020

You can set the responses parameter at the router level: https://fastapi.tiangolo.com/tutorial/bigger-applications/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found