Skip to content

Commit 32c76bf

Browse files
authored
Only create class instance after validation in ModelValidator.validate_construct() (#1818)
1 parent 8d06fac commit 32c76bf

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

src/validators/model.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,12 @@ impl ModelValidator {
300300
}
301301
}
302302

303-
let instance = create_class(self.class.bind(py))?;
303+
let instance;
304304

305305
if self.root_model {
306306
let state = &mut state.rebind_extra(|extra| extra.field_name = Some(PyString::new(py, ROOT_FIELD)));
307307
let output = self.validator.validate(py, input, state)?;
308+
instance = create_class(self.class.bind(py))?;
308309

309310
let fields_set = if input.as_python().is_some_and(|py_input| py_input.is(&self.undefined)) {
310311
PySet::empty(py)?
@@ -315,6 +316,7 @@ impl ModelValidator {
315316
force_setattr(py, &instance, intern!(py, ROOT_FIELD), output)?;
316317
} else {
317318
let output = self.validator.validate(py, input, state)?;
319+
instance = create_class(self.class.bind(py))?;
318320

319321
let (model_dict, model_extra, val_fields_set): (Bound<PyAny>, Bound<PyAny>, Bound<PyAny>) =
320322
output.extract(py)?;

0 commit comments

Comments
 (0)