Skip to content

Commit

Permalink
Merge pull request fastapi#13 from AndrewRiggs-Atkins/init-validation…
Browse files Browse the repository at this point in the history
…-kwarg

feat: addition of optional validate
  • Loading branch information
leynier authored Jun 8, 2022
2 parents 9c220a5 + d793def commit c2dc053
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sqlmodel/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,14 @@ def get_config(name: str) -> Any:
# other future tools based on Pydantic can use.
new_cls.__config__.read_with_orm_mode = True

config_validate = get_config("validate")
if config_validate is True:
# If it was passed by kwargs, ensure it's also set in config
new_cls.__config__.validate = config_validate
for k, v in new_cls.__fields__.items():
col = get_column_from_field(v)
setattr(new_cls, k, col)

config_registry = get_config("registry")
if config_registry is not Undefined:
config_registry = cast(registry, config_registry)
Expand Down Expand Up @@ -507,10 +515,12 @@ def __init__(__pydantic_self__, **data: Any) -> None:
)
# Only raise errors if not a SQLModel model
if (
not getattr(__pydantic_self__.__config__, "table", False)
(not getattr(__pydantic_self__.__config__, "table", False)
or getattr(__pydantic_self__.__config__, "validate", False))
and validation_error
):
raise validation_error
raise validation_error
# Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy
# can handle them
# object.__setattr__(__pydantic_self__, '__dict__', values)
Expand Down

0 comments on commit c2dc053

Please sign in to comment.