Skip to content

Commit

Permalink
Fix corner case of empty items array (#871)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbalboni authored and glasserc committed Jan 18, 2019
1 parent f27a24d commit f72be02
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,12 @@ function computeDefaults(schema, parentDefaults, definitions = {}) {
if (schema.minItems > defaultsLength) {
const defaultEntries = defaults || [];
// populate the array with the defaults
const fillerSchema = Array.isArray(schema.items)
? schema.additionalItems
: schema.items;
const fillerEntries = fill(
new Array(schema.minItems - defaultsLength),
computeDefaults(schema.items, schema.items.defaults, definitions)
computeDefaults(fillerSchema, fillerSchema.defaults, definitions)
);
// then fill up the rest with either the item default or empty, up to minItems

Expand Down
20 changes: 20 additions & 0 deletions test/utils_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ describe("utils", () => {
foo: 42,
});
});

it("should fill array with additional items schema when items is empty", () => {
const schema = {
type: "object",
properties: {
array: {
type: "array",
minItems: 1,
additionalItems: {
type: "string",
default: "foo",
},
items: [],
},
},
};
expect(getDefaultFormState(schema, {})).eql({
array: ["foo"],
});
});
});
});

Expand Down

0 comments on commit f72be02

Please sign in to comment.