Skip to content

Commit

Permalink
fix(store): error messages for empty array and nested objects
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed Sep 6, 2024
1 parent 213e176 commit bd9dc13
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,12 @@ function setupModel(Model, nested) {
if (isArray) {
const nestedType = typeof defaultValue[0];

if (nestedType === "undefined") {
throw TypeError(
`The first item of the '${key}' array must be defined`,
);
}

if (nestedType !== "object") {
if (
nestedType === "function" &&
Expand Down Expand Up @@ -648,6 +654,12 @@ function setupModel(Model, nested) {
};
}

if (Object.keys(defaultValue).length === 0) {
throw TypeError(
`The object for the '${key}' must have at least one property`,
);
}

const nestedConfig = bootstrap(defaultValue, true);
if (nestedConfig.enumerable || nestedConfig.external) {
if (
Expand Down
8 changes: 8 additions & 0 deletions test/spec/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,14 @@ describe("store:", () => {
expect(() => store.get({ nested: [() => {}] })).toThrow();
});

it("throws when nested array don't have a first item", () => {
expect(() => store.get({ nested: [] })).toThrow();
});

it("throws when nested object has no properties defined", () => {
expect(() => store.get({ nested: {} })).toThrow();
});

it("set to an error state when get method returning undefined", () => {
Model = {
value: "test",
Expand Down

0 comments on commit bd9dc13

Please sign in to comment.