是否可以调用所有验证器来获取完整的错误列表?
@validator('password', always=True)
def validate_password1(cls, value):
password = value.get_secret_value()
min_length = 8
if len(password) < min_length:
raise ValueError('Password must be at least 8 characters long.')
return value
@validator('password', always=True)
def validate_password2(cls, value):
password = value.get_secret_value()
if not any(character.islower() for character in password):
raise ValueError('Password should contain at least one lowercase character.')
return value
Run Code Online (Sandbox Code Playgroud)
当前的行为似乎一次调用一个验证器。
我的 Pydantic 课程:
class User(BaseModel):
email: EmailStr
password: SecretStr
Run Code Online (Sandbox Code Playgroud)
如果我没有在请求中包含email, 或password, 字段,那么我会在数组中得到两个验证失败,这就是我想要对该字段执行的操作password,但当前的行为似乎调用了一个,如果失败则抛出立即出现错误。
所以我有以下代码行:
item: Optional[int] = Field(None, ge=1, le=168)
Run Code Online (Sandbox Code Playgroud)
我也希望有可能设定-1价值。因此,我需要排除零值,但我想允许一个值和来自to-1的值。1168
有什么办法可以做到这一点吗?
我有以下 FastAPI 后端:
from fastapi import FastAPI
app = FastAPI
class Demo(BaseModel):
content: str = None
@app.post("/demo")
async def demoFunc(d:Demo):
return d.content
Run Code Online (Sandbox Code Playgroud)
问题是,当我向此 API 发送带有额外数据的请求时,例如:
data = {"content":"some text here"}aaaa
Run Code Online (Sandbox Code Playgroud)
或者
data = {"content":"some text here"aaaaaa}
resp = requests.post(url, json=data)
Run Code Online (Sandbox Code Playgroud)
422 unprocessable entity在以下情况下,它会抛出状态代码错误,返回字段中包含 Actual("some text here") 和 Extra("aaaaa") 数据data = {"content":"some text here"}aaaa:
{
"detail": [
{
"loc": [
"body",
47
],
"msg": "Extra data: line 4 column 2 (char 47)",
"type": "value_error.jsondecode",
"ctx": {
"msg": "Extra …Run Code Online (Sandbox Code Playgroud) 我正在为 Discord 制作一个 rick roll 网站,我想重定向到404响应状态代码的 rick roll 页面。
我尝试了以下方法,但没有成功:
@app.exception_handler(fastapi.HTTPException)
async def http_exception_handler(request, exc):
...
Run Code Online (Sandbox Code Playgroud)