Skip to content

Commit

Permalink
fix: omit computedDefault of empty objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Rani committed May 3, 2022
1 parent 5a69458 commit 907945e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/core/src/utils.js
Expand Up @@ -3,6 +3,7 @@ import * as ReactIs from "react-is";
import mergeAllOf from "json-schema-merge-allof";
import fill from "core-js-pure/features/array/fill";
import union from "lodash/union";
import isEmpty from "lodash/isEmpty";
import jsonpointer from "jsonpointer";
import fields from "./components/fields";
import widgets from "./components/widgets";
Expand Down Expand Up @@ -244,7 +245,7 @@ function computeDefaults(
(formData || {})[key],
includeUndefinedValues
);
if (includeUndefinedValues || computedDefault !== undefined) {
if (includeUndefinedValues || !isEmpty(computedDefault)) {
acc[key] = computedDefault;
}
return acc;
Expand Down
25 changes: 25 additions & 0 deletions packages/core/test/utils_test.js
Expand Up @@ -1093,6 +1093,31 @@ describe("utils", () => {
});
});

it("should not create empty objects a default value", () => {
const schema = {
type: "object",
required: [],
properties: {
value: {
type: "object",
required: ["a"],
properties: {
a: {
type: "string",
},
b: {
type: "string",
},
c: {
type: "string",
},
},
},
},
};
expect(getDefaultFormState(schema, {})).eql({});
});

it("should not crash for defaults for nested dependencies when formData passed to computeDefaults is null", () => {
const schema = {
type: "object",
Expand Down

0 comments on commit 907945e

Please sign in to comment.