Skip to content

Commit

Permalink
fix: ignore nested object validation if the parent object does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Oct 27, 2023
1 parent aa2a308 commit 2c41fab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
34 changes: 32 additions & 2 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ describe('class(Validator)', () => {
},
});

expect(validator.check({})).toEqual(['"messages.hello" is not defined']);
expect(
validator.check({
messages: {},
})
).toEqual(['"messages.hello" is not defined']);
});

it('should support any number of nested objects', () => {
Expand All @@ -49,7 +53,33 @@ describe('class(Validator)', () => {
},
});

expect(validator.check({})).toEqual(['"i18n.en.hello" is not defined']);
expect(
validator.check({
i18n: {
en: {},
},
})
).toEqual(['"i18n.en.hello" is not defined']);
});

it('should ignore validation if a parent object does not exist', () => {
type Example = {
i18n: {
en: {
hello: string;
};
};
};

const validator = new Validator<Example>({
i18n: {
en: {
hello: [isDefined],
},
},
});

expect(validator.check({})).toEqual([]);
});

it('should support ensuring arrays exist', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class Validator<T extends object> {
config: configItem,
});
}
} else {
} else if (innerValue) {
stack.push({
parentKey: innerKey,
item: innerValue,
Expand Down

0 comments on commit 2c41fab

Please sign in to comment.