Skip to content

Commit 53426e4

Browse files
committed
throws error if validate fn not found
Signed-off-by: Antonio Mendoza Pérez <antmendoza@gmail.com>
1 parent 2dfb446 commit 53426e4

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/lib/utils.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import { validators } from './validators';
99
*/
1010
export const validate = (typeName: string, data: any): boolean => {
1111
const validateFn: ValidateFunction | undefined = validators.get(typeName);
12-
// TODO: ignore validation if no validator or throw ?
13-
if (!validateFn) return data;
12+
13+
if (!validateFn) {
14+
throw Error(`Validate function not defined for type [${typeName}]`);
15+
};
16+
17+
1418
if (!validateFn(data)) {
1519
console.warn(validateFn.errors);
1620
const firstError: DefinedError = (validateFn.errors as DefinedError[])[0];

tests/lib/utils.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ describe('validate', () => {
88
it('should return true for valid objects', () => {
99
expect(validate('End', false)).toBeTruthy('Expected function validate to return true for valid objects');
1010
});
11+
12+
it('should throws an error if validator not found', () => {
13+
expect( () => validate('ValidatorNotDefined', {} )).toThrowError();
14+
});
15+
1116
});

0 commit comments

Comments
 (0)