@@ -9,7 +9,7 @@ use pyo3::PyVisit;
9
9
use super :: { build_validator, BuildValidator , CombinedValidator , DefinitionsBuilder , ValidationState , Validator } ;
10
10
use crate :: build_tools:: py_schema_err;
11
11
use crate :: build_tools:: schema_or_config_same;
12
- use crate :: errors:: { LocItem , ValError , ValResult } ;
12
+ use crate :: errors:: { ErrorTypeDefaults , LocItem , ValError , ValResult } ;
13
13
use crate :: input:: Input ;
14
14
use crate :: py_gc:: PyGcTraverse ;
15
15
use crate :: tools:: SchemaDict ;
@@ -180,6 +180,11 @@ impl Validator for WithDefaultValidator {
180
180
outer_loc : Option < impl Into < LocItem > > ,
181
181
state : & mut ValidationState < ' _ , ' py > ,
182
182
) -> ValResult < Option < PyObject > > {
183
+ if matches ! ( self . default , DefaultType :: DefaultFactory ( _, true ) ) && state. has_field_error {
184
+ // The default factory might use data from fields that failed to validate, and this results
185
+ // in an unhelpul error.
186
+ return Err ( ValError :: new ( ErrorTypeDefaults :: DefaultFactoryNotCalled , input) ) ;
187
+ }
183
188
match self . default . default_value ( py, state. extra ( ) . data . as_ref ( ) ) ? {
184
189
Some ( stored_dft) => {
185
190
let dft: Py < PyAny > = if self . copy_default {
0 commit comments