Skip to content

Commit d793def

Browse files
Addition of optional validate kwarg for validating object on instantiation even if not a table.
1 parent 8e97c93 commit d793def

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

sqlmodel/main.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,14 @@ def get_config(name: str) -> Any:
303303
# other future tools based on Pydantic can use.
304304
new_cls.__config__.read_with_orm_mode = True
305305

306+
config_validate = get_config("validate")
307+
if config_validate is True:
308+
# If it was passed by kwargs, ensure it's also set in config
309+
new_cls.__config__.validate = config_validate
310+
for k, v in new_cls.__fields__.items():
311+
col = get_column_from_field(v)
312+
setattr(new_cls, k, col)
313+
306314
config_registry = get_config("registry")
307315
if config_registry is not Undefined:
308316
config_registry = cast(registry, config_registry)
@@ -498,10 +506,12 @@ def __init__(__pydantic_self__, **data: Any) -> None:
498506
)
499507
# Only raise errors if not a SQLModel model
500508
if (
501-
not getattr(__pydantic_self__.__config__, "table", False)
509+
(not getattr(__pydantic_self__.__config__, "table", False)
510+
or getattr(__pydantic_self__.__config__, "validate", False))
502511
and validation_error
503512
):
504513
raise validation_error
514+
raise validation_error
505515
# Do not set values as in Pydantic, pass them through setattr, so SQLAlchemy
506516
# can handle them
507517
# object.__setattr__(__pydantic_self__, '__dict__', values)

0 commit comments

Comments
 (0)