Skip to content

Commit a8a58d1

Browse files
committed
1 parent 57f8bb2 commit a8a58d1

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

.changeset/weak-spies-matter.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/form": patch
3+
---
4+
5+
Port <https://github.com/rjsf-team/react-jsonschema-form/pull/4425> fix

packages/form/src/core/default-state.test.ts

-5
Original file line numberDiff line numberDiff line change
@@ -3334,11 +3334,6 @@ describe("getDefaultFormState2()", () => {
33343334
).toEqual({
33353335
baseRequiredProperty: {
33363336
requiredProperty: "foo",
3337-
// NOTE: this part of data is not present in original test
3338-
// because original code ignores falsy defaults
3339-
optionalProperty: {
3340-
nestedRequiredProperty: "",
3341-
},
33423337
},
33433338
});
33443339
});

packages/form/src/core/default-state.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,13 @@ function maybeAddDefaultToObject(
555555
obj.set(key, computedDefault);
556556
}
557557
} else if (emptyObjectFields !== "skipDefaults") {
558-
if (isSchemaObjectValue(computedDefault)) {
559-
// If isParentRequired is undefined, then we are at the root level of the schema so defer to the requiredness of
560-
// the field key itself in the `requiredField` list
561-
const isSelfOrParentRequired = isSchemaRoot
562-
? requiredFields.has(key)
563-
: isParentRequired;
558+
// If isParentRequired is undefined, then we are at the root level of the schema so defer to the requiredness of
559+
// the field key itself in the `requiredField` list
560+
const isSelfOrParentRequired = isSchemaRoot
561+
? requiredFields.has(key)
562+
: isParentRequired;
564563

564+
if (isSchemaObjectValue(computedDefault)) {
565565
// If emptyObjectFields 'skipEmptyDefaults' store computedDefault if it's a non-empty object(e.g. not {})
566566
if (emptyObjectFields === "skipEmptyDefaults") {
567567
if (!isSchemaValueEmpty(computedDefault)) {
@@ -585,7 +585,7 @@ function maybeAddDefaultToObject(
585585
computedDefault !== undefined &&
586586
(emptyObjectFields === "populateAllDefaults" ||
587587
emptyObjectFields === "skipEmptyDefaults" ||
588-
requiredFields.has(key))
588+
(isSelfOrParentRequired && requiredFields.has(key)))
589589
) {
590590
obj.set(key, computedDefault);
591591
}

0 commit comments

Comments
 (0)