Skip to content

Commit a6903ad

Browse files
authored
Merge pull request #120 from antmendoza/throw-error-if-validator-not-found
Throws error if validator function not found
2 parents c620a72 + 7721a04 commit a6903ad

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Diff for: package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: src/lib/utils.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,11 @@ import { validators } from './validators';
2424
*/
2525
export const validate = (typeName: string, data: any): boolean => {
2626
const validateFn: ValidateFunction | undefined = validators.get(typeName);
27-
// TODO: ignore validation if no validator or throw ?
28-
if (!validateFn) return data;
27+
28+
if (!validateFn) {
29+
throw Error(`Validate function not defined for type '${typeName}'`);
30+
}
31+
2932
if (!validateFn(data)) {
3033
console.warn(validateFn.errors);
3134
const firstError: DefinedError = (validateFn.errors as DefinedError[])[0];

Diff for: tests/lib/utils.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,8 @@ describe('validate', () => {
2424
it('should return true for valid objects', () => {
2525
expect(validate('End', false)).toBeTruthy('Expected function validate to return true for valid objects');
2626
});
27+
28+
it('should throws an error if validator not found', () => {
29+
expect(() => validate('ValidatorNotDefined', {})).toThrowError();
30+
});
2731
});

0 commit comments

Comments
 (0)