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
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())):
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 aField required
validation error and the custom validators are run (which would is the default behaviour for theBaseModel
classes).Current Behavior
running this returns
Expected behavior
Expected would be the output that pydantic generates you if you derive directly from
BaseModel
without running the custom validators at all:Environment
python -c "import pydantic.utils; print(pydantic.utils.version_info())
):The text was updated successfully, but these errors were encountered: