Skip to content

Commit

Permalink
Merge pull request #120 from antmendoza/throw-error-if-validator-not-…
Browse files Browse the repository at this point in the history
…found

Throws error if validator function not found
  • Loading branch information
antmendoza authored Jul 13, 2021
2 parents c620a72 + 7721a04 commit a6903ad
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ import { validators } from './validators';
*/
export const validate = (typeName: string, data: any): boolean => {
const validateFn: ValidateFunction | undefined = validators.get(typeName);
// TODO: ignore validation if no validator or throw ?
if (!validateFn) return data;

if (!validateFn) {
throw Error(`Validate function not defined for type '${typeName}'`);
}

if (!validateFn(data)) {
console.warn(validateFn.errors);
const firstError: DefinedError = (validateFn.errors as DefinedError[])[0];
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ describe('validate', () => {
it('should return true for valid objects', () => {
expect(validate('End', false)).toBeTruthy('Expected function validate to return true for valid objects');
});

it('should throws an error if validator not found', () => {
expect(() => validate('ValidatorNotDefined', {})).toThrowError();
});
});

0 comments on commit a6903ad

Please sign in to comment.