Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
eadwinCode committed Oct 10, 2023
1 parent a5dbd2a commit 4e25708
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions ninja_schema/orm/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import warnings

from django.db.models import Model as DjangoModel
from pydantic.functional_validators import ModelWrapValidatorHandler

from ..pydanticutils import IS_PYDANTIC_V1
from ..types import DictStrAny
Expand All @@ -23,10 +24,26 @@ def apply_to_model(
from pydantic_core.core_schema import ValidationInfo

class BaseMixinsV2(BaseMixins):
@model_validator(mode="before")
def _run_root_validator(cls, values: t.Any, info: ValidationInfo) -> t.Any:
@model_validator(mode="wrap")
@classmethod
def _run_root_validator(
cls,
values: t.Any,
handler: ModelWrapValidatorHandler[t.Any],
info: ValidationInfo,
) -> t.Any:
# when extra is "forbid" we need to perform default pydantic valudation
# as DjangoGetter does not act as dict and pydantic will not be able to validate it
# if cls.model_config.get("extra") == "forbid": #type:ignore[attr-defined]
# handler(values)

values = DjangoGetter(values, cls, info.context)
return values
return handler(values)

# @model_validator(mode="before")
# def _run_root_validator(cls, values: t.Any, info: ValidationInfo) -> t.Any:
# values = DjangoGetter(values, cls, info.context)
# return values

@classmethod
def from_orm(cls, obj: t.Any, **options: t.Any) -> BaseModel:
Expand Down

0 comments on commit 4e25708

Please sign in to comment.