例如,我不想在 FastAPI Swagger 文档中公开字符串的正确正则表达式。如何实现这一目标?
我实际上是通过以下方式实现的:
@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
)
Run Code Online (Sandbox Code Playgroud)
但它仅在我从 Postman 调用端点时才起作用,但在 Swagger 中,FastAPI 似乎甚至在到达端点之前就抛出验证错误。
我尝试了很多事情,但似乎没有任何效果。