From bbc3bbb10bf4efbdba31032afe62ed57212d0125 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Mendoza=20P=C3=A9rez?= Date: Sat, 10 Jul 2021 08:29:07 +0200 Subject: [PATCH] ignore additionalProperties when are declared as function 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/definitions/callbackstate.ts | 4 +-- src/lib/definitions/databasedswitch.ts | 4 +-- src/lib/definitions/defaultdef.ts | 4 +-- src/lib/definitions/delaystate.ts | 4 +-- src/lib/definitions/end.ts | 4 +-- src/lib/definitions/enddatacondition.ts | 4 +-- src/lib/definitions/enddeventcondition.ts | 4 +-- src/lib/definitions/error.ts | 4 +-- src/lib/definitions/eventbasedswitch.ts | 4 +-- src/lib/definitions/eventdef.ts | 4 +-- src/lib/definitions/eventstate.ts | 4 +-- src/lib/definitions/exectimeout.ts | 4 +-- src/lib/definitions/foreachstate.ts | 4 +-- src/lib/definitions/function.ts | 5 ++-- src/lib/definitions/injectstate.ts | 4 +-- src/lib/definitions/onevents.ts | 4 +-- src/lib/definitions/operationstate.ts | 4 +-- src/lib/definitions/parallelstate.ts | 4 +-- src/lib/definitions/repeat.ts | 4 +-- src/lib/definitions/subflowstate.ts | 4 +-- src/lib/definitions/transition.ts | 4 +-- .../definitions/transitiondatacondition.ts | 4 +-- .../definitions/transitioneventcondition.ts | 4 +-- src/lib/definitions/workflow.ts | 6 ++--- src/lib/utils.ts | 2 +- tests/lib/utils.spec.ts | 25 +++++++++++++++++++ 27 files changed, 78 insertions(+), 52 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/definitions/callbackstate.ts b/src/lib/definitions/callbackstate.ts index 9301436..0dd9590 100644 --- a/src/lib/definitions/callbackstate.ts +++ b/src/lib/definitions/callbackstate.ts @@ -110,7 +110,7 @@ export class Callbackstate { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Callbackstate} without deleted properties. */ - normalize(): Callbackstate { + normalize = (): Callbackstate => { const clone = new Callbackstate(this); normalizeUsedForCompensationProperty(clone); @@ -121,5 +121,5 @@ export class Callbackstate { setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/databasedswitch.ts b/src/lib/definitions/databasedswitch.ts index 52b59b9..34d9f14 100644 --- a/src/lib/definitions/databasedswitch.ts +++ b/src/lib/definitions/databasedswitch.ts @@ -84,7 +84,7 @@ export class Databasedswitch { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Databasedswitch} without deleted properties. */ - normalize(): Databasedswitch { + normalize = (): Databasedswitch => { const clone = new Databasedswitch(this); normalizeUsedForCompensationProperty(clone); @@ -92,5 +92,5 @@ export class Databasedswitch { normalizeDataConditionsProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/defaultdef.ts b/src/lib/definitions/defaultdef.ts index 4053085..f2db2bc 100644 --- a/src/lib/definitions/defaultdef.ts +++ b/src/lib/definitions/defaultdef.ts @@ -39,7 +39,7 @@ export class Defaultdef /* Default definition. Can be either a transition or end * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Defaultdef} without deleted properties. */ - normalize(): Defaultdef { + normalize = (): Defaultdef => { const clone = new Defaultdef(this); normalizeEndProperty(clone); @@ -47,5 +47,5 @@ export class Defaultdef /* Default definition. Can be either a transition or end setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/delaystate.ts b/src/lib/definitions/delaystate.ts index 899cba8..13daa0b 100644 --- a/src/lib/definitions/delaystate.ts +++ b/src/lib/definitions/delaystate.ts @@ -93,7 +93,7 @@ export class Delaystate { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Delaystate} without deleted properties. */ - normalize(): Delaystate { + normalize = (): Delaystate => { const clone = new Delaystate(this); normalizeUsedForCompensationProperty(clone); @@ -102,5 +102,5 @@ export class Delaystate { normalizeTransitionProperty(clone); setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/end.ts b/src/lib/definitions/end.ts index 8a0a6ee..5248ec4 100644 --- a/src/lib/definitions/end.ts +++ b/src/lib/definitions/end.ts @@ -45,12 +45,12 @@ export class End { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.End} without deleted properties. */ - normalize(): End { + normalize = (): End => { const clone = new End(this); normalizeCompensateProperty(clone); normalizeTerminateProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/enddatacondition.ts b/src/lib/definitions/enddatacondition.ts index c704d8f..7028993 100644 --- a/src/lib/definitions/enddatacondition.ts +++ b/src/lib/definitions/enddatacondition.ts @@ -44,11 +44,11 @@ export class Enddatacondition { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Enddatacondition} without deleted properties. */ - normalize(): Enddatacondition { + normalize = (): Enddatacondition => { const clone = new Enddatacondition(this); normalizeEndProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/enddeventcondition.ts b/src/lib/definitions/enddeventcondition.ts index 12d09b5..cbdde16 100644 --- a/src/lib/definitions/enddeventcondition.ts +++ b/src/lib/definitions/enddeventcondition.ts @@ -56,11 +56,11 @@ export class Enddeventcondition { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Enddeventcondition} without deleted properties. */ - normalize(): Enddeventcondition { + normalize = (): Enddeventcondition => { const clone = new Enddeventcondition(this); normalizeEndProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/error.ts b/src/lib/definitions/error.ts index 18f4d83..ee7b2f9 100644 --- a/src/lib/definitions/error.ts +++ b/src/lib/definitions/error.ts @@ -51,7 +51,7 @@ export class Error { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Error} without deleted properties. */ - normalize(): Error { + normalize = (): Error => { const clone = new Error(this); normalizeEndProperty(clone); @@ -60,5 +60,5 @@ export class Error { setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/eventbasedswitch.ts b/src/lib/definitions/eventbasedswitch.ts index 10c133d..135a41d 100644 --- a/src/lib/definitions/eventbasedswitch.ts +++ b/src/lib/definitions/eventbasedswitch.ts @@ -91,7 +91,7 @@ export class Eventbasedswitch { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Eventbasedswitch} without deleted properties. */ - normalize(): Eventbasedswitch { + normalize = (): Eventbasedswitch => { const clone = new Eventbasedswitch(this); normalizeUsedForCompensationProperty(clone); @@ -99,5 +99,5 @@ export class Eventbasedswitch { normalizeEventConditionsProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/eventdef.ts b/src/lib/definitions/eventdef.ts index 627120b..d2f24d3 100644 --- a/src/lib/definitions/eventdef.ts +++ b/src/lib/definitions/eventdef.ts @@ -58,11 +58,11 @@ export class Eventdef { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Eventdef} without deleted properties. */ - normalize(): Eventdef { + normalize = (): Eventdef => { const clone = new Eventdef(this); normalizeKindProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/eventstate.ts b/src/lib/definitions/eventstate.ts index edeaf46..f95b594 100644 --- a/src/lib/definitions/eventstate.ts +++ b/src/lib/definitions/eventstate.ts @@ -89,7 +89,7 @@ export class Eventstate /* This state is used to wait for events from event sour * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Eventstate} without deleted properties. */ - normalize(): Eventstate { + normalize = (): Eventstate => { const clone = new Eventstate(this); normalizeExclusiveProperty(clone); @@ -100,5 +100,5 @@ export class Eventstate /* This state is used to wait for events from event sour setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/exectimeout.ts b/src/lib/definitions/exectimeout.ts index 5162a9a..d474866 100644 --- a/src/lib/definitions/exectimeout.ts +++ b/src/lib/definitions/exectimeout.ts @@ -39,11 +39,11 @@ export class Exectimeout { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Exectimeout} without deleted properties. */ - normalize(): Exectimeout { + normalize = (): Exectimeout => { const clone = new Exectimeout(this); normalizeInterruptProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/foreachstate.ts b/src/lib/definitions/foreachstate.ts index f430ef7..5a767ef 100644 --- a/src/lib/definitions/foreachstate.ts +++ b/src/lib/definitions/foreachstate.ts @@ -113,7 +113,7 @@ export class Foreachstate { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Foreachstate} without deleted properties. */ - normalize(): Foreachstate { + normalize = (): Foreachstate => { const clone = new Foreachstate(this); normalizeUsedForCompensationProperty(clone); @@ -123,5 +123,5 @@ export class Foreachstate { setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/function.ts b/src/lib/definitions/function.ts index a661a42..18645af 100644 --- a/src/lib/definitions/function.ts +++ b/src/lib/definitions/function.ts @@ -40,11 +40,12 @@ export class Function { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Function} without deleted properties. */ - normalize(): Function { + + normalize = (): Function => { const clone = new Function(this); normalizeTypeRestProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/injectstate.ts b/src/lib/definitions/injectstate.ts index 014b272..17effff 100644 --- a/src/lib/definitions/injectstate.ts +++ b/src/lib/definitions/injectstate.ts @@ -84,7 +84,7 @@ export class Injectstate { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Injectstate} without deleted properties. */ - normalize(): Injectstate { + normalize = (): Injectstate => { const clone = new Injectstate(this); normalizeUsedForCompensationProperty(clone); @@ -94,5 +94,5 @@ export class Injectstate { setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/onevents.ts b/src/lib/definitions/onevents.ts index d2dc1e2..6795c0b 100644 --- a/src/lib/definitions/onevents.ts +++ b/src/lib/definitions/onevents.ts @@ -48,11 +48,11 @@ export class Onevents { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Onevents} without deleted properties. */ - normalize(): Onevents { + normalize = (): Onevents => { const clone = new Onevents(this); normalizeActionModeParallelProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/operationstate.ts b/src/lib/definitions/operationstate.ts index e657d77..e2ab502 100644 --- a/src/lib/definitions/operationstate.ts +++ b/src/lib/definitions/operationstate.ts @@ -102,7 +102,7 @@ export class Operationstate { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Operationstate} without deleted properties. */ - normalize(): Operationstate { + normalize = (): Operationstate => { const clone = new Operationstate(this); normalizeActionModeSequentialProperty(clone); @@ -113,5 +113,5 @@ export class Operationstate { setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/parallelstate.ts b/src/lib/definitions/parallelstate.ts index d339993..f0f5457 100644 --- a/src/lib/definitions/parallelstate.ts +++ b/src/lib/definitions/parallelstate.ts @@ -106,7 +106,7 @@ export class Parallelstate { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Parallelstate} without deleted properties. */ - normalize(): Parallelstate { + normalize = (): Parallelstate => { const clone = new Parallelstate(this); normalizeCompletionTypeProperty(clone); @@ -118,5 +118,5 @@ export class Parallelstate { setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/repeat.ts b/src/lib/definitions/repeat.ts index b71f8e8..7a690cb 100644 --- a/src/lib/definitions/repeat.ts +++ b/src/lib/definitions/repeat.ts @@ -51,12 +51,12 @@ export class Repeat { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Repeat} without deleted properties. */ - normalize(): Repeat { + normalize = (): Repeat => { const clone = new Repeat(this); normalizeContinueOnErrorProperty(clone); normalizeCheckBeforeProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/subflowstate.ts b/src/lib/definitions/subflowstate.ts index 45bd0a2..6602b1f 100644 --- a/src/lib/definitions/subflowstate.ts +++ b/src/lib/definitions/subflowstate.ts @@ -105,7 +105,7 @@ export class Subflowstate { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Subflowstate} without deleted properties. */ - normalize(): Subflowstate { + normalize = (): Subflowstate => { const clone = new Subflowstate(this); normalizeUsedForCompensationProperty(clone); @@ -120,5 +120,5 @@ export class Subflowstate { setEndValueIfNoTransition(clone); return clone; - } + }; } diff --git a/src/lib/definitions/transition.ts b/src/lib/definitions/transition.ts index e2d25f0..5f10c0c 100644 --- a/src/lib/definitions/transition.ts +++ b/src/lib/definitions/transition.ts @@ -44,11 +44,11 @@ export class Transition { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Transition} without deleted properties. */ - normalize(): Transition { + normalize = (): Transition => { const clone = new Transition(this); normalizeCompensateProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/transitiondatacondition.ts b/src/lib/definitions/transitiondatacondition.ts index 1b79fe1..9562179 100644 --- a/src/lib/definitions/transitiondatacondition.ts +++ b/src/lib/definitions/transitiondatacondition.ts @@ -44,11 +44,11 @@ export class Transitiondatacondition { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Transitiondatacondition} without deleted properties. */ - normalize(): Transitiondatacondition { + normalize = (): Transitiondatacondition => { const clone = new Transitiondatacondition(this); normalizeTransitionProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/transitioneventcondition.ts b/src/lib/definitions/transitioneventcondition.ts index 53017fd..efd7f4d 100644 --- a/src/lib/definitions/transitioneventcondition.ts +++ b/src/lib/definitions/transitioneventcondition.ts @@ -55,11 +55,11 @@ export class Transitioneventcondition { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Transitioneventcondition} without deleted properties. */ - normalize(): Transitioneventcondition { + normalize = (): Transitioneventcondition => { const clone = new Transitioneventcondition(this); normalizeTransitionProperty(clone); return clone; - } + }; } diff --git a/src/lib/definitions/workflow.ts b/src/lib/definitions/workflow.ts index be6d3a2..d63529d 100644 --- a/src/lib/definitions/workflow.ts +++ b/src/lib/definitions/workflow.ts @@ -98,7 +98,7 @@ export class Workflow { * Normalize the value of each property by recursively deleting properties whose value is equal to its default value. Does not modify the object state. * @returns {Specification.Workflow} without deleted properties. */ - normalize(): Workflow { + normalize = (): Workflow => { const clone = new Workflow(this); normalizeKeepActiveProperty(clone); @@ -111,7 +111,7 @@ export class Workflow { normalizeExecTimeout(clone); return clone; - } + }; /** * Parses the provided string as Workflow @@ -144,6 +144,6 @@ export class Workflow { */ static toYaml(workflow: Workflow): string { validate('Workflow', workflow); - return yaml.dump(workflow.normalize()); + return yaml.dump(JSON.parse(JSON.stringify(workflow.normalize()))); } } diff --git a/src/lib/utils.ts b/src/lib/utils.ts index cc7feca..d8f7cce 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -11,7 +11,7 @@ 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(data)) { + if (!validateFn(JSON.parse(JSON.stringify(data)))) { console.warn(validateFn.errors); const firstError: DefinedError = (validateFn.errors as DefinedError[])[0]; throw new Error( diff --git a/tests/lib/utils.spec.ts b/tests/lib/utils.spec.ts index 1d8f179..efe9361 100644 --- a/tests/lib/utils.spec.ts +++ b/tests/lib/utils.spec.ts @@ -8,4 +8,29 @@ 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 ignore "normalize" function as additionalProperty', () => { + const functionObj = { + name: 'function', + operation: 'operation', + type: 'rest', + normalize: () => { + //do something + }, + }; + + expect(validate('Function', functionObj)).toBeTruthy(); + }); + + it('should NOT ignore additionalProperties', () => { + const functionObj = { + name: 'function', + operation: 'operation', + type: 'rest', + keyAdditionalProperty: 'anyValue', + }; + + expect(() => validate('Function', functionObj)).toThrowError(/keyAdditionalProperty/); + }); + });