@@ -202,6 +202,7 @@ impl Validator for ModelValidator {
202202 field_name. to_string ( ) ,
203203 ) )
204204 } else {
205+ let state = & mut state. rebind_extra ( |extra| extra. field_name = Some ( PyString :: new ( py, ROOT_FIELD ) ) ) ;
205206 let output = self . validator . validate ( py, field_value, state) ?;
206207
207208 force_setattr ( py, model, intern ! ( py, ROOT_FIELD ) , output) ?;
@@ -255,9 +256,11 @@ impl ModelValidator {
255256 // we need to set `self_instance` to None for nested validators as we don't want to operate on self_instance
256257 // anymore
257258 let state = & mut state. rebind_extra ( |extra| extra. self_instance = None ) ;
258- let output = self . validator . validate ( py, input, state) ?;
259259
260260 if self . root_model {
261+ let state = & mut state. rebind_extra ( |extra| extra. field_name = Some ( PyString :: new ( py, ROOT_FIELD ) ) ) ;
262+ let output = self . validator . validate ( py, input, state) ?;
263+
261264 let fields_set = if input. as_python ( ) . is_some_and ( |py_input| py_input. is ( & self . undefined ) ) {
262265 PySet :: empty ( py) ?
263266 } else {
@@ -266,6 +269,8 @@ impl ModelValidator {
266269 force_setattr ( py, self_instance, intern ! ( py, DUNDER_FIELDS_SET_KEY ) , & fields_set) ?;
267270 force_setattr ( py, self_instance, intern ! ( py, ROOT_FIELD ) , & output) ?;
268271 } else {
272+ let output = self . validator . validate ( py, input, state) ?;
273+
269274 let ( model_dict, model_extra, fields_set) : ( Bound < PyAny > , Bound < PyAny > , Bound < PyAny > ) =
270275 output. extract ( py) ?;
271276 set_model_attrs ( self_instance, & model_dict, & model_extra, & fields_set) ?;
@@ -294,11 +299,12 @@ impl ModelValidator {
294299 }
295300 }
296301
297- let output = self . validator . validate ( py, input, state) ?;
298-
299302 let instance = create_class ( self . class . bind ( py) ) ?;
300303
301304 if self . root_model {
305+ let state = & mut state. rebind_extra ( |extra| extra. field_name = Some ( PyString :: new ( py, ROOT_FIELD ) ) ) ;
306+ let output = self . validator . validate ( py, input, state) ?;
307+
302308 let fields_set = if input. as_python ( ) . is_some_and ( |py_input| py_input. is ( & self . undefined ) ) {
303309 PySet :: empty ( py) ?
304310 } else {
@@ -307,6 +313,8 @@ impl ModelValidator {
307313 force_setattr ( py, & instance, intern ! ( py, DUNDER_FIELDS_SET_KEY ) , & fields_set) ?;
308314 force_setattr ( py, & instance, intern ! ( py, ROOT_FIELD ) , output) ?;
309315 } else {
316+ let output = self . validator . validate ( py, input, state) ?;
317+
310318 let ( model_dict, model_extra, val_fields_set) : ( Bound < PyAny > , Bound < PyAny > , Bound < PyAny > ) =
311319 output. extract ( py) ?;
312320 let fields_set = existing_fields_set. unwrap_or ( & val_fields_set) ;
0 commit comments