Is each field meant to be validated by at most one validator? #4087
-
I've just tried the following with Pydantic 1.9.0. from pydantic import *
class TestModel(BaseModel):
x: StrictStr
@validator("x")
def validate_x_one(cls, value):
print("validator 1")
return ValueError("bad 1")
@validator("x")
def validate_x_two(cls, value):
print("validator 2")
raise ValueError("bad 2")
TestModel(x="") Pasting this into an interpreter, I see:
I was surprised that (For context, I was hoping to use Pydantic to validate a config file. I wanted to have multiple small, independent checks on a given field so that they each be reported to the user in one batch.) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
I made a typo: from pydantic import *
class TestModel(BaseModel):
x: StrictStr
@validator("x")
def validate_x_one(cls, value):
print("validator 1")
raise ValueError("bad 1")
@validator("x")
def validate_x_two(cls, value):
print("validator 2")
raise ValueError("bad 2")
TestModel(x="") This now yields:
which makes more sense. However, it seems like the first error is reported and the second validator is never run. Is there some way to guarantee that both validators run and have both of their errors reported? |
Beta Was this translation helpful? Give feedback.
-
Ahh, judging by https://github.com/samuelcolvin/pydantic/blob/8846ec4685e749b93907081450f592060eeb99b1/pydantic/fields.py#L1116-L1124 it looks like we stop validating a field as soon as one of its validators reports an error. |
Beta Was this translation helpful? Give feedback.
Ahh, judging by https://github.com/samuelcolvin/pydantic/blob/8846ec4685e749b93907081450f592060eeb99b1/pydantic/fields.py#L1116-L1124 it looks like we stop validating a field as soon as one of its validators reports an error.