Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected @field_validator behaviour #445

Open
lowmann15 opened this issue Apr 1, 2024 · 0 comments
Open

Unexpected @field_validator behaviour #445

lowmann15 opened this issue Apr 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@lowmann15
Copy link

Bug

If you create a mandatory attribute for a custom class that derives from Model and create a instance of this class without providing the mandatory field, you do not get a Field required validation error and the custom validators are run (which would is the default behaviour for the BaseModel classes).

Current Behavior

import datetime
from typing import Optional

from odmantic import Field, Model
from pydantic import field_validator


class MyClass(Model):
    date_of_birth: datetime.datetime
    birth_year: Optional[int] = Field(None)

    @field_validator("birth_year", mode="before")
    def validate_birth_year(cls, v: int, values) -> int:
        """Validates the birth year."""
        if v is None:
            date_of_birth: datetime = values.data.get("date_of_birth")
            v = date_of_birth.year
        return v

running this returns

v = date_of_birth.year
        ^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'year'

Expected behavior

Expected would be the output that pydantic generates you if you derive directly from BaseModel without running the custom validators at all:

pydantic_core._pydantic_core.ValidationError: 1 validation error for MyClass
date_of_birth
  Field required [type=missing, input_value={}, input_type=dict]
    For further information visit https://errors.pydantic.dev/2.6/v/missing

Environment

  • ODMantic version: 1.0.1
  • Pydantic infos (output of python -c "import pydantic.utils; print(pydantic.utils.version_info())):
 pydantic version: 2.6.4
        pydantic-core version: 2.16.3
          pydantic-core build: profile=release pgo=true
                 install path: /Users/lxr1046/Library/Caches/pypoetry/virtualenvs/clinical-etl-service-lM9J4W3w-py3.11/lib/python3.11/site-packages/pydantic
               python version: 3.11.8 (main, Feb  6 2024, 21:21:21) [Clang 15.0.0 (clang-1500.1.0.2.5)]
                     platform: macOS-14.4-arm64-arm-64bit
             related packages: fastapi-0.110.0 mypy-1.9.0 typing_extensions-4.10.0 pydantic-settings-2.2.1
                       commit: unknown

@lowmann15 lowmann15 added the bug Something isn't working label Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant