@@ -31,9 +31,9 @@ def name(self):
31
31
else :
32
32
return self .type
33
33
34
- def is_valid_update (self , new_order : list [Any ]) -> list [str ]:
34
+ def is_valid_update (self , new_order : list [Any ]) -> dict [ str , list [str ] ]:
35
35
"""
36
- Returns a list of validation warnings without updating the current object
36
+ Returns a dict of validation warnings without updating the current object
37
37
"""
38
38
new_obj_dict = self .model_dump ()
39
39
new_obj_dict ["order" ] = new_order
@@ -82,6 +82,7 @@ def validate_order(cls, value: list[Any]) -> list[Any]:
82
82
@model_validator (mode = "after" )
83
83
def validate_sortable_consistency (self ) -> "Sortable" :
84
84
"""Additional validation for the entire sortable"""
85
+ # Example: validate that certain types require subtypes
85
86
if self .type == "heat_network" and self .subtype is None :
86
87
raise ValueError ("heat_network type requires a subtype" )
87
88
@@ -103,36 +104,20 @@ def from_json(
103
104
sort_type , payload = data
104
105
105
106
if isinstance (payload , list ):
106
- try :
107
- sortable = cls .model_validate ({"type" : sort_type , "order" : payload })
108
- yield sortable
109
- except Exception as e :
110
- # Create basic sortable with warning
111
- sortable = cls .model_validate ({"type" : sort_type , "order" : []})
112
- sortable .add_warning ('base' , f"Failed to create sortable for { sort_type } : { e } " )
113
- yield sortable
107
+ sortable = cls (type = sort_type , order = payload )
108
+ yield sortable
114
109
115
110
elif isinstance (payload , dict ):
116
111
for sub , order in payload .items ():
117
- try :
118
- sortable = cls .model_validate (
119
- {"type" : sort_type , "subtype" : sub , "order" : order }
120
- )
121
- yield sortable
122
- except Exception as e :
123
- # Create basic sortable with warning
124
- sortable = cls .model_validate (
125
- {"type" : sort_type , "subtype" : sub , "order" : []}
126
- )
127
- sortable .add_warning (
128
- 'base' , f"Failed to create sortable for { sort_type } .{ sub } : { e } "
129
- )
130
- yield sortable
112
+ sortable = cls (type = sort_type , subtype = sub , order = order )
113
+ yield sortable
131
114
132
115
else :
133
116
# Create basic sortable with warning for unexpected payload
134
- sortable = cls .model_validate ({"type" : sort_type , "order" : []})
135
- sortable .add_warning ('type' , f"Unexpected payload for '{ sort_type } ': { payload !r} " )
117
+ sortable = cls (type = sort_type , order = [])
118
+ sortable .add_warning (
119
+ "payload" , f"Unexpected payload for '{ sort_type } ': { payload !r} "
120
+ )
136
121
yield sortable
137
122
138
123
@@ -182,8 +167,8 @@ def is_valid_update(self, updates: Dict[str, list[Any]]) -> Dict[str, list[str]]
182
167
# Check for non-existent sortables
183
168
non_existent_names = set (updates .keys ()) - set (self .names ())
184
169
for name in non_existent_names :
185
- if name not in warnings :
186
- warnings [name ] = [f "Sortable { name } does not exist" ]
170
+ if name not in warnings : # Don't overwrite existing warnings
171
+ warnings [name ] = ["Sortable does not exist" ]
187
172
188
173
return warnings
189
174
@@ -243,13 +228,15 @@ def from_json(cls, data: Dict[str, Any]) -> "Sortables":
243
228
for pair in data .items ():
244
229
items .extend (Sortable .from_json (pair ))
245
230
246
- collection = cls .model_validate ({"sortables" : items })
231
+ # Use Base class constructor that handles validation gracefully
232
+ collection = cls (sortables = items )
247
233
248
234
# Merge any warnings from individual sortables
249
235
for sortable in items :
250
236
if hasattr (sortable , "warnings" ) and sortable .warnings :
251
- for warning in sortable .warnings :
252
- collection .add_warning (warning )
237
+ for warning_key , warning_list in sortable .warnings .items ():
238
+ for warning in warning_list :
239
+ collection .add_warning (f"Sortable.{ warning_key } " , warning )
253
240
254
241
return collection
255
242
0 commit comments