For example, I don't want to expose the correct regex for a string in the FastAPI Swagger docs. How to achieve that?
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.