我正在使用 pydantic 进行架构验证,当任何未定义的额外字段添加到架构中时,我想抛出错误。
from typing import Literal, Union
from pydantic import BaseModel, Field, ValidationError
class Cat(BaseModel):
pet_type: Literal['cat']
meows: int
class Dog(BaseModel):
pet_type: Literal['dog']
barks: float
class Lizard(BaseModel):
pet_type: Literal['reptile', 'lizard']
scales: bool
class Model(BaseModel):
pet: Union[Cat, Dog, Lizard] = Field(..., discriminator='pet_type')
n: int
print(Model(pet={'pet_type': 'dog', 'barks': 3.14, 'eats': 'biscuit'}, n=1))
""" try:
Model(pet={'pet_type': 'dog'}, n=1)
except ValidationError as e:
print(e) """
在上面的代码中,我添加了未定义的 eats
字段。应用 pydantic 验证并删除我定义的额外值作为响应。我想抛出一个错误,指出 eats is not allowed for Dog
或类似的内容。有什么办法可以实现这一点吗?
我们是否有机会直接提供输入而不是 pet
对象?
print(Model({'pet_type': 'dog', 'barks': 3.14, 'eats': 'biscuit', n=1}))
.我尝试不使用鉴别器
,但缺少与pet_type
相关的特定验证。有人可以指导我如何实现其中之一吗?
I am using pydantic for schema validations and I would like to throw an error when any extra field that isn't defined is added to a schema.
from typing import Literal, Union
from pydantic import BaseModel, Field, ValidationError
class Cat(BaseModel):
pet_type: Literal['cat']
meows: int
class Dog(BaseModel):
pet_type: Literal['dog']
barks: float
class Lizard(BaseModel):
pet_type: Literal['reptile', 'lizard']
scales: bool
class Model(BaseModel):
pet: Union[Cat, Dog, Lizard] = Field(..., discriminator='pet_type')
n: int
print(Model(pet={'pet_type': 'dog', 'barks': 3.14, 'eats': 'biscuit'}, n=1))
""" try:
Model(pet={'pet_type': 'dog'}, n=1)
except ValidationError as e:
print(e) """
In the above code, I have added the eats
field which is not defined. The pydantic validations are applied and the extra values that I defined are removed in response. I want to throw an error saying eats is not allowed for Dog
or something like that. Is there any way to achieve that?
And is there any chance that we can provide the input directly instead of the pet
object?
print(Model({'pet_type': 'dog', 'barks': 3.14, 'eats': 'biscuit', n=1}))
. I tried without descriminator
but those specific validations are missing related to pet_type
. Can someone guide me how to achieve either one of that?
发布评论
评论(3)
Pydantic v2
您可以使用
额外
字段rel="noreferrer">model_config
类属性用于在模型初始化期间禁止额外属性(默认情况下,额外属性将被忽略 )。例如:
将引发
ValidationError
:当模型“嵌套”时也有效,例如:
将引发:
NB: 如您所见,
extra
的类型为ExtraValues现在,并且它的值将由 ConfigDict 进行验证。这意味着不可能意外地为
extra
提供不受支持的值(例如输入错误),即ConfigDict(extra="fordib")
之类的内容将失败并显示 < a href="https://docs.pydantic.dev/latest/api/pydantic_core/#pydantic_core.SchemaError" rel="noreferrer">SchemaError
。Pydantic v1
您可以使用
extra
字段Config
类在模型初始化期间禁止额外的属性(默认情况下,额外的属性将被忽略)。例如:
将引发
VaidationError
:当模型“嵌套”时也有效,例如:
将引发:
Pydantic v2
You can use the
extra
field in themodel_config
class attribute to forbid extra attributes during model initialisation (by default, additional attributes will be ignored).For example:
will raise a
ValidationError
:Works as well when the model is "nested", e.g.:
will raise:
NB: As you can see,
extra
has the typeExtraValues
now, and its value will get validated byConfigDict
. This means it's not possible to accidentally provide an unsupported value forextra
(e.g. having a typo), i.e. something likeConfigDict(extra="fordib")
will fail with aSchemaError
.Pydantic v1
You can use the
extra
field in theConfig
class to forbid extra attributes during model initialisation (by default, additional attributes will be ignored).For example:
will raise a
VaidationError
:Works as well when the model is "nested", e.g.:
will raise:
首选解决方案是使用 ConfigDict(参考文档):
Paul P 的回答 仍然有效(目前),但
Config
类已经在 pydantic v2.0 中已弃用。另一个已弃用的解决方案是 pydantic.Extra.forbid。
上述方法的优点之一是可以进行类型检查。
The preferred solution is to use a
ConfigDict
(ref. the documentation):Paul P's answer still works (for now), but the
Config
class has been deprecated in pydantic v2.0.Another deprecated solution is
pydantic.Extra.forbid
.One advantage of the method above is that it can be type checked.
Pydantic是为了通过模式来验证您的输入。在您的情况下,您要删除其验证功能之一。
我认为您应该创建一个从
basemodel
继承的新类,然后创建您的验证类
,现在可以创建一个新的
cat
,而不必担心未定义的属性。像这个第二个问题一样,要直接从
dict
进行输入,您可以使用双星号**
或
Pydantic is made to validate your input with the schema. In your case, you want to remove one of its validation feature.
I think you should create a new class that inherit from
BaseModel
then create your validation classes
now you can create a new
Cat
without worries about undefined attribute. Like this2nd question, to put the input directly from
dict
you can use double asterisk**
or