Skip to content

Commit

Permalink
simplified forbid-extra handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalik committed Sep 25, 2023
1 parent 18f3ebc commit 914e84a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
26 changes: 3 additions & 23 deletions ninja/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,38 +204,18 @@ class Config:
def _run_root_validator(
cls, values: Any, handler: ModelWrapValidatorHandler[S], info: ValidationInfo
) -> S:
# We dont perform 'before' validations if an validating through 'model_validate'
through_model_validate = (
info and info.context and info.context.get("through_model_validate", False)
)
if not through_model_validate:
# 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":
handler(values)

# We add our DjangoGetter for the Schema
values = DjangoGetter(values, cls, info.context)

# To update the schema with our DjangoGetter
return handler(values)

@classmethod
def from_orm(cls: Type[S], obj: Any) -> S:
return cls.model_validate(obj)

@classmethod
def model_validate(
cls: Type[S],
obj: Any,
*,
strict: Optional[bool] = None,
from_attributes: Optional[bool] = None,
context: Optional[Dict[str, Any]] = None,
) -> S:
context = context or {}
context["through_model_validate"] = True
return super().model_validate(
obj, strict=strict, from_attributes=from_attributes, context=context
)

def dict(self, *a: Any, **kw: Any) -> DictStrAny:
"Backward compatibility with pydantic 1.x"
return self.model_dump(*a, **kw)
Expand Down
9 changes: 9 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ dev = [
"pre-commit",
]


# BLACK ==========================================================

[tool.black]
line-length = 88
skip-string-normalization = false
target-version = ['py311']


[tool.ruff]
select = [
"E", # pycodestyle errors
Expand Down

0 comments on commit 914e84a

Please sign in to comment.