小编Sha*_*hag的帖子

FastAPI:如何获取 OpenAPI 中的自定义验证错误?

例如,我不想在 FastAPI Swagger 文档中公开字符串的正确正则表达式。如何实现这一目标?

招摇验证错误: 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 似乎甚至在到达端点之前就抛出验证错误。

我尝试了很多事情,但似乎没有任何效果。

python swagger openapi fastapi

5
推荐指数
0
解决办法
624
查看次数

标签 统计

fastapi ×1

openapi ×1

python ×1

swagger ×1