From 53426e4a230f248c01d8306f4bb82ef1534410b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Mendoza=20P=C3=A9rez?= Date: Sun, 11 Jul 2021 08:05:35 +0200 Subject: [PATCH 1/3] throws error if validate fn not found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Antonio Mendoza Pérez --- src/lib/utils.ts | 8 ++++++-- tests/lib/utils.spec.ts | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index cc7feca..a563417 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -9,8 +9,12 @@ 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]; diff --git a/tests/lib/utils.spec.ts b/tests/lib/utils.spec.ts index 1d8f179..73ff50d 100644 --- a/tests/lib/utils.spec.ts +++ b/tests/lib/utils.spec.ts @@ -8,4 +8,9 @@ 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(); + }); + }); From 3f68aa6d52422679c750b8a8dc276e64db4811f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Mendoza=20P=C3=A9rez?= Date: Sun, 11 Jul 2021 08:06:28 +0200 Subject: [PATCH 2/3] code formating MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Antonio Mendoza Pérez --- src/lib/utils.ts | 3 +-- tests/lib/utils.spec.ts | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lib/utils.ts b/src/lib/utils.ts index a563417..62fd6a0 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -12,8 +12,7 @@ export const validate = (typeName: string, data: any): boolean => { if (!validateFn) { throw Error(`Validate function not defined for type [${typeName}]`); - }; - + } if (!validateFn(data)) { console.warn(validateFn.errors); diff --git a/tests/lib/utils.spec.ts b/tests/lib/utils.spec.ts index 73ff50d..6b96b40 100644 --- a/tests/lib/utils.spec.ts +++ b/tests/lib/utils.spec.ts @@ -10,7 +10,6 @@ describe('validate', () => { }); it('should throws an error if validator not found', () => { - expect( () => validate('ValidatorNotDefined', {} )).toThrowError(); + expect(() => validate('ValidatorNotDefined', {})).toThrowError(); }); - }); From 7721a04c87f03ecd2f1cc66301c76f4486482e48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Mendoza=20P=C3=A9rez?= Date: Tue, 13 Jul 2021 11:14:15 +0200 Subject: [PATCH 3/3] changed error message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Antonio Mendoza Pérez --- package-lock.json | 4 ++-- src/lib/utils.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 51a157f..6b60c0d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@severlessworkflow/sdk-typescript", - "version": "0.6.0", + "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@severlessworkflow/sdk-typescript", - "version": "0.6.0", + "version": "1.0.0", "license": "http://www.apache.org/licenses/LICENSE-2.0.txt", "dependencies": { "ajv": "^8.1.0", diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 5bfc712..61d134c 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -26,7 +26,7 @@ export const validate = (typeName: string, data: any): boolean => { const validateFn: ValidateFunction | undefined = validators.get(typeName); if (!validateFn) { - throw Error(`Validate function not defined for type [${typeName}]`); + throw Error(`Validate function not defined for type '${typeName}'`); } if (!validateFn(data)) {