Skip to content

Current field value is not displayed in after field validator #8126

@MaxHalford

Description

@MaxHalford

Initial Checks

  • I confirm that I'm using Pydantic V2

Description

I have a before field validator which populates a field based on another field. I then have an after field validator which checks the obtained value. The resolution order of these validators is correct. The issue I see is that in the error message, the input_value displays the current value of the field, it instead displays what I assume is the starting value.

This isn't a blocking issue. But I believe modifying the message to display the current value of the field would be more useful.

I'm happy to provide more details if needed :)

Example Code

Here's the MRE:

import pydantic
import typing
from typing_extensions import Annotated
from pydantic import BaseModel, ValidationInfo, field_validator
from pydantic.dataclasses import dataclass


class Base(BaseModel):
    raw: str
    norm: typing.Any | None = None

    @field_validator('norm')
    def normalize_raw_if_no_norm(cls, norm: str | None, info: ValidationInfo):
        if norm is None:
            norm = cls.normalize_raw(info['model'].raw)
        return norm

@dataclass
class Weight:
    raw: str | None = None
    norm: Annotated[typing.Any | None, pydantic.Field(validate_default=True)] = None

    @pydantic.field_validator("norm", mode="before")
    def normalize_raw_if_no_norm(cls, norm: str | None, info: ValidationInfo):
        print("normalize_raw_if_no_norm")
        if norm is None:
            if (raw := info.data.get("raw")):
                norm = cls.normalize_raw(info.data["raw"])
            else:
                raise ValueError("Either raw or norm has to be provided")
        return norm

    @pydantic.field_validator("norm")
    def check_positive(cls, norm):
        print(norm)
        assert norm >= 0, "Weight has to be positive"
        return norm

    @classmethod
    def normalize_raw(cls, raw: str):
        return float(raw)

Weight(raw='-32')

Here's the exception:

ValidationError: 1 validation error for Weight
norm
  Assertion failed, Weight has to be positive [type=assertion_error, input_value=None, input_type=NoneType]
    For further information visit https://errors.pydantic.dev/2.5/v/assertion_error

As you can see, the input_value is None and not -32.0. This is what I would expect:

ValidationError: 1 validation error for Weight
norm
  Assertion failed, Weight has to be positive [type=assertion_error, input_value=-32.0, input_type=float]
    For further information visit https://errors.pydantic.dev/2.5/v/assertion_error

Python, Pydantic & OS Version

pydantic version: 2.5.0
        pydantic-core version: 2.14.1
          pydantic-core build: profile=release pgo=true
                 install path: /Users/max/Library/Caches/pypoetry/virtualenvs/vera-aGmBzZyk-py3.10/lib/python3.10/site-packages/pydantic
               python version: 3.10.8 (main, Nov 10 2022, 15:09:18) [Clang 14.0.0 (clang-1400.0.29.102)]
                     platform: macOS-14.0-arm64-arm-64bit
             related packages: fastapi-0.100.0 typing_extensions-4.6.3

Metadata

Metadata

Assignees

Labels

bug V2Bug related to Pydantic V2

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions