pydantic无法区分整数和字符串
Integer and string cannot be distinguished with pydantic
from pydantic import BaseModel
class AuthenticationResponseSchema(BaseModel):
type: str
schema = AuthenticationResponseSchema(type=1)
现在我正在将 marshmallow 更改为 pydantic 的架构、模型...
但是 pydantic 模式在数据响应时没有验证 type
。
type
的数据类型是字符串,但也传递了整数。
怎么了?
谢谢。
中的 statad
strings are accepted as-is, int float and Decimal are coerced using str(v), bytes and bytearray are converted using v.decode(), enums inheriting from str are converted using v.value, and all other types cause an error
如果你想强制字符串,有一个叫做Strict Types
的东西,所以你可以使用StrictStr
。
from pydantic import BaseModel, StrictStr
class AuthenticationResponseSchema(BaseModel):
type: StrictStr
schema = AuthenticationResponseSchema(type=1)
from pydantic import BaseModel
class AuthenticationResponseSchema(BaseModel):
type: str
schema = AuthenticationResponseSchema(type=1)
现在我正在将 marshmallow 更改为 pydantic 的架构、模型...
但是 pydantic 模式在数据响应时没有验证 type
。
type
的数据类型是字符串,但也传递了整数。
怎么了?
谢谢。
strings are accepted as-is, int float and Decimal are coerced using str(v), bytes and bytearray are converted using v.decode(), enums inheriting from str are converted using v.value, and all other types cause an error
如果你想强制字符串,有一个叫做Strict Types
的东西,所以你可以使用StrictStr
。
from pydantic import BaseModel, StrictStr
class AuthenticationResponseSchema(BaseModel):
type: StrictStr
schema = AuthenticationResponseSchema(type=1)