@@ -115,11 +115,11 @@ function maybeAddDefaultToObject<T = any>(
115115 // Or if the schema has a const property defined, then we should always return the computedDefault since it's coming from the const.
116116 obj [ key ] = computedDefault ;
117117 } else if ( emptyObjectFields !== 'skipDefaults' ) {
118- if ( isObject ( computedDefault ) ) {
119- // If isParentRequired is undefined, then we are at the root level of the schema so defer to the requiredness of
120- // the field key itself in the `requiredField` list
121- const isSelfOrParentRequired = isParentRequired === undefined ? requiredFields . includes ( key ) : isParentRequired ;
118+ // If isParentRequired is undefined, then we are at the root level of the schema so defer to the requiredness of
119+ // the field key itself in the `requiredField` list
120+ const isSelfOrParentRequired = isParentRequired === undefined ? requiredFields . includes ( key ) : isParentRequired ;
122121
122+ if ( isObject ( computedDefault ) ) {
123123 // If emptyObjectFields 'skipEmptyDefaults' store computedDefault if it's a non-empty object(e.g. not {})
124124 if ( emptyObjectFields === 'skipEmptyDefaults' ) {
125125 if ( ! isEmpty ( computedDefault ) ) {
@@ -138,11 +138,12 @@ function maybeAddDefaultToObject<T = any>(
138138 } else if (
139139 // Store computedDefault if it's a defined primitive (e.g., true) and satisfies certain conditions
140140 // Condition 1: computedDefault is not undefined
141- // Condition 2: If emptyObjectFields is 'populateAllDefaults' or 'skipEmptyDefaults) or if the key is a required field
141+ // Condition 2: If emptyObjectFields is 'populateAllDefaults' or 'skipEmptyDefaults)
142+ // Or if isSelfOrParentRequired is 'true' and the key is a required field
142143 computedDefault !== undefined &&
143144 ( emptyObjectFields === 'populateAllDefaults' ||
144145 emptyObjectFields === 'skipEmptyDefaults' ||
145- requiredFields . includes ( key ) )
146+ ( isSelfOrParentRequired && requiredFields . includes ( key ) ) )
146147 ) {
147148 obj [ key ] = computedDefault ;
148149 }
0 commit comments