Skip to content

Commit

Permalink
feat: added strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
cecilia-sanare committed Oct 27, 2023
1 parent 2c41fab commit 13ae06e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ describe('class(Validator)', () => {
expect(validator.check({})).toEqual([]);
});

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

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

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

it('should support ensuring arrays exist', () => {
type Example = {
arr: string[];
Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export class Validator<T extends object> {
private config: Validator.Config<T>;
constructor(config: Validator.Config<T>) {
private strict: boolean;
constructor(config: Validator.Config<T>, strict: boolean = false) {
this.config = config;
this.strict = strict;
}

check(thing: object): string[] {
Expand Down Expand Up @@ -50,7 +52,7 @@ export class Validator<T extends object> {
config: configItem,
});
}
} else if (innerValue) {
} else if (this.strict || innerValue) {
stack.push({
parentKey: innerKey,
item: innerValue,
Expand Down

0 comments on commit 13ae06e

Please sign in to comment.