Skip to content

Commit e24cb0e

Browse files
committed
Enhance PydanticJSONB to support Dict and List type hints for model validation
1 parent 5f3adaf commit e24cb0e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

sqlmodel/sql/sqltypes.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,25 @@ def process_result_value(
6060
if value is None:
6161
return None
6262
if isinstance(value, dict):
63-
# If model_class is dict, handle key-value pairs
64-
if isinstance(self.model_class, dict):
65-
return {k: self.model_class.model_validate(v) for k, v in value.items()}
63+
# If model_class is a Dict type hint, handle key-value pairs
64+
if (
65+
hasattr(self.model_class, "__origin__")
66+
and self.model_class.__origin__ is dict
67+
):
68+
model_class = get_args(self.model_class)[
69+
1
70+
] # Get the value type (the model)
71+
return {k: model_class.model_validate(v) for k, v in value.items()}
6672
# Regular case: the whole dict represents a single model
6773
return self.model_class.model_validate(value) # type: ignore
6874
if isinstance(value, list):
69-
return [get_args(self.model_class)[0].model_validate(v) for v in value] # type: ignore
75+
# If model_class is a List type hint
76+
if (
77+
hasattr(self.model_class, "__origin__")
78+
and self.model_class.__origin__ is list
79+
):
80+
model_class = get_args(self.model_class)[0]
81+
return [model_class.model_validate(v) for v in value]
82+
# Fallback case (though this shouldn't happen given our __init__ types)
83+
return [self.model_class.model_validate(v) for v in value] # type: ignore
7084
return value

0 commit comments

Comments
 (0)