If a model has more than one failing @model_validator("after")
, then only one (the first?) is raised.
Is it possible to tell Pydantic to run all model validators? Are multiple model validators even supported? The docs seem to be silent on this question.
In the below example, I would expect two validation errors, but Pydantic only raises one:
<code>from pydantic import BaseModel, model_validator
class Foo(BaseModel):
x: int
y: str
z: float
@model_validator(mode="after")
def x_and_y(self):
if self.x == 1 and self.y == "2":
raise ValueError("x and y")
return self
@model_validator(mode="after")
def y_and_z(self):
if self.y == "2" and int(self.z) == 3:
raise ValueError("y and z")
return self
# This should fail both validators, but only one is raised:
Foo(x=1, y="2", z=3.0)
# pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
# Value error, x and y [type=value_error, input_value={'x': 1, 'y': '2', 'z': 3.0},
# input_type=dict]
# For further information visit https://errors.pydantic.dev/2.7/v/value_error
# If the first validator passes, only then the second one is reported
Foo(x=100, y="2", z=3.0)
# pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
# Value error, y and z [type=value_error, input_value={'x': 100, 'y': '2', 'z': 3.0}, # input_type=dict]
# For further information visit https://errors.pydantic.dev/2.7/v/value_erro
</code>
<code>from pydantic import BaseModel, model_validator
class Foo(BaseModel):
x: int
y: str
z: float
@model_validator(mode="after")
def x_and_y(self):
if self.x == 1 and self.y == "2":
raise ValueError("x and y")
return self
@model_validator(mode="after")
def y_and_z(self):
if self.y == "2" and int(self.z) == 3:
raise ValueError("y and z")
return self
# This should fail both validators, but only one is raised:
Foo(x=1, y="2", z=3.0)
# pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
# Value error, x and y [type=value_error, input_value={'x': 1, 'y': '2', 'z': 3.0},
# input_type=dict]
# For further information visit https://errors.pydantic.dev/2.7/v/value_error
# If the first validator passes, only then the second one is reported
Foo(x=100, y="2", z=3.0)
# pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
# Value error, y and z [type=value_error, input_value={'x': 100, 'y': '2', 'z': 3.0}, # input_type=dict]
# For further information visit https://errors.pydantic.dev/2.7/v/value_erro
</code>
from pydantic import BaseModel, model_validator
class Foo(BaseModel):
x: int
y: str
z: float
@model_validator(mode="after")
def x_and_y(self):
if self.x == 1 and self.y == "2":
raise ValueError("x and y")
return self
@model_validator(mode="after")
def y_and_z(self):
if self.y == "2" and int(self.z) == 3:
raise ValueError("y and z")
return self
# This should fail both validators, but only one is raised:
Foo(x=1, y="2", z=3.0)
# pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
# Value error, x and y [type=value_error, input_value={'x': 1, 'y': '2', 'z': 3.0},
# input_type=dict]
# For further information visit https://errors.pydantic.dev/2.7/v/value_error
# If the first validator passes, only then the second one is reported
Foo(x=100, y="2", z=3.0)
# pydantic_core._pydantic_core.ValidationError: 1 validation error for Foo
# Value error, y and z [type=value_error, input_value={'x': 100, 'y': '2', 'z': 3.0}, # input_type=dict]
# For further information visit https://errors.pydantic.dev/2.7/v/value_erro