微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 GET 请求中使用 pydantic 验证器

如何解决在 GET 请求中使用 pydantic 验证器

我正在尝试验证数据库中已有的数据。

我有这个 Pydantic 模型:

class Schema(BaseModel):
    name: str

    @validator("name")
    def name_must_not_contain_spaces(cls,v):
        if ' ' in v:
            raise ValueError("must not contain space")
        return v

这是返回所有对象的 crud 操作的 get_all

# crud.py
async def get_all() -> List:
    objects = await TestModel.all().values()
    return objects

# TestModel is a Model created with Tortoise-orm. 

这是 GET 端点:

@router.get('/',response_model=List[Schema])
async def get_all_tests() -> List[Schema]:
    return await crud.get_all()

当我尝试 POST 时,它运行良好,在 json 中返回引发的错误

但是,当我尝试验证数据库中已有的数据时,出现内部服务器错误,并且在终端上有此引用:

ERROR:uvicorn.error:Exception in Asgi application
Traceback (most recent call last):
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/uvicorn/protocols/http/h11_impl.py",line 396,in run_asgi
    result = await app(self.scope,self.receive,self.send)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/uvicorn/middleware/proxy_headers.py",line 45,in __call__
    return await self.app(scope,receive,send)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/fastapi/applications.py",line 199,in __call__
    await super().__call__(scope,send)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/applications.py",line 112,in __call__
    await self.middleware_stack(scope,send)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py",line 181,in __call__
    raise exc from None
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/middleware/errors.py",line 159,in __call__
    await self.app(scope,_send)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/exceptions.py",line 82,in __call__
    raise exc from None
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/exceptions.py",line 71,sender)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/routing.py",line 580,in __call__
    await route.handle(scope,send)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/starlette/routing.py",line 241,in handle
    await self.app(scope,line 52,in app
    response = await func(request)
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/fastapi/routing.py",line 209,in app
    response_data = await serialize_response(
  File "/Users/Library/Caches/pypoetry/virtualenvs/fastapi-version-bfOHfHp9-py3.8/lib/python3.8/site-packages/fastapi/routing.py",line 126,in serialize_response
    raise ValidationError(errors,field.type_)
pydantic.error_wrappers.ValidationError: 1 validation error for Schema
response -> 5 -> name
  must not contain space (type=value_error)

我想要正是这个错误,但作为响应正文中显示的json响应

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?