我想从 pydantic 模型类更改验证消息,模型类的代码如下:
class Input(BaseModel):
ip: IPvAnyAddress
@validator("ip", always=True)
def not_valid_ip(cls, v):
"""To validate ip-address."""
if str(v) == "":
raise ValueError(f'Invalid IP-Address:: {v}')
if not isinstance(v, IPvAnyAddress):
raise ValueError(f'Invalid IP format:: {v}')
return v
目前,它不会更新上面写的消息。我正在使用 fastAPI 进行 API 开发。
{
"detail": [
{
"loc": [
"body",
"input",
"ip"
],
"msg": "value is not a valid IPv4 or IPv6 address",
"type": "value_error.ipvanyaddress"
}
]
}