You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When validating a class that has field_validators for optional fields that have validate_default=True, only one error gets returned based on the first field_validator. None of the basic validation errors get returned (like missing), and no sequential field_validators get called.
Given a model with optional fields with default values, validation of defaults, and field validators:
When called with no arguments we only get 1 validation error. The first field_validator.
ExampleKO()
"""pydantic_core._pydantic_core.ValidationError: 1 validation error for ExampleKOvalue_a Assertion failed, [type=assertion_error, input_value=None, input_type=NoneType] For further information visit https://errors.pydantic.dev/2.4/v/assertion_error"""
I would expect the above to behave the same as if I passed two None values for those optional fields:
ExampleKO(value_a=None, value_b=None)
"""value_0 Field required [type=missing, input_value={'value_a': None, 'value_b': None}, input_type=dict] For further information visit https://errors.pydantic.dev/2.4/v/missingvalue_a Assertion failed, [type=assertion_error, input_value=None, input_type=NoneType] For further information visit https://errors.pydantic.dev/2.4/v/assertion_errorvalue_b Assertion failed, [type=assertion_error, input_value=None, input_type=NoneType] For further information visit https://errors.pydantic.dev/2.4/v/assertion_error"""
Example Code
fromtypingimportAnnotated, OptionalfrompydanticimportBaseModel, Field, field_validatorclassExampleKO(BaseModel):
value_0: strvalue_a: Annotated[Optional[str], Field(None, validate_default=True)]
value_b: Annotated[Optional[str], Field(None, validate_default=True)]
@field_validator("value_a", mode="after")defvalue_a_validator(cls, value):
raiseAssertionError@field_validator("value_b", mode="after")defvalue_b_validator(cls, value):
raiseAssertionErrorExampleKO()
"""pydantic_core._pydantic_core.ValidationError: 1 validation error for V2ExampleKOvalue_a Assertion failed, [type=assertion_error, input_value=None, input_type=NoneType] For further information visit https://errors.pydantic.dev/2.4/v/assertion_error"""
We don't have an issue there - your filing of this issue alerted us to this bug. I'll have a PR up shortly in both pydantic-core and pydantic working on this fix.
Thanks again for bringing this issue to our attention 😄
Initial Checks
Description
When validating a class that has
field_validator
s for optional fields that havevalidate_default=True
, only one error gets returned based on the firstfield_validator
. None of the basic validation errors get returned (likemissing
), and no sequentialfield_validator
s get called.Given a model with optional fields with default values, validation of defaults, and field validators:
When called with no arguments we only get 1 validation error. The first
field_validator
.I would expect the above to behave the same as if I passed two None values for those optional fields:
Example Code
Python, Pydantic & OS Version
The text was updated successfully, but these errors were encountered: