5

For example, I don't want to expose the correct regex for a string in the FastAPI Swagger docs. How to achieve that?

Swagger Validation Error: Swagger Validation Error

I have actually achieved it using the following way:

@app.exception_handler(RequestValidationError)
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    return PlainTextResponse(
        str("the string does not match the correct regex"), status_code=422
    )

BUT it works only when I call the endpoint from Postman, but in Swagger, FastAPI seems to throw the validation error even before hitting the endpoint.

I have tried lots of things but nothing seem to work.

Chris
  • 18,724
  • 6
  • 46
  • 80

0 Answers0