diff --git a/README.md b/README.md index 66dc58d1..5c71fb30 100644 --- a/README.md +++ b/README.md @@ -139,30 +139,31 @@ The sdk provides a way to validate if a workflow object is compliant with the se - `validate(): boolean` ```typescript -import { WorkflowValidator, Specification } from '@severlessworkflow/sdk-typescript'; - -const workflow: Specification.Workflow = { - id: 'helloworld', - version: '1.0', - specVersion: '0.8', - name: 'Hello World Workflow', - description: 'Inject Hello World', - start: 'Hello State', - states: [ - { - type: 'inject', - name: 'Hello State', - end: true, - data: { - result: "Hello World!" - } - } as Specification.Injectstate - ] +import {WorkflowValidator, Specification} from '@severlessworkflow/sdk-typescript'; +import {Workflow} from "./workflow"; + +const workflow = { + id: 'helloworld', + version: '1.0', + specVersion: '0.3', + name: 'Hello World Workflow', + description: 'Inject Hello World', + start: 'Hello State', + states: [ + { + type: 'inject', + name: 'Hello State', + end: true, + data: { + result: "Hello World!" + } + } + ] }; -const workflowValidator: WorkflowValidator = new WorkflowValidator(workflow); +const workflowValidator: WorkflowValidator = new WorkflowValidator(Workflow.fromSource(JSON.stringify(workflow))); if (!workflowValidator.isValid) { - workflowValidator.errors.forEach(error => console.error((error as ValidationError).message)); + workflowValidator.errors.forEach(error => console.error((error as ValidationError).message)); } ``` diff --git a/src/lib/workflow-validator.ts b/src/lib/workflow-validator.ts index 6214822e..1faca005 100644 --- a/src/lib/workflow-validator.ts +++ b/src/lib/workflow-validator.ts @@ -34,10 +34,12 @@ export class WorkflowValidator { */ constructor(private workflow: Specification.Workflow) { const validateFn = validators.get('Workflow') as ValidateFunction; - validateFn(this.workflow); + const normalizedWf = this.workflow.normalize(); + validateFn(JSON.parse(JSON.stringify(normalizedWf))); if (validateFn.errors) { this.errors = validateFn.errors.map((error) => { - const message = `invalid: ${error.instancePath} | ${error.schemaPath} | ${error.message}`; + const message = `Workflow is invalid: ${error.instancePath} | ${error.schemaPath} | ${error.message} + data: ${JSON.stringify(normalizedWf, null, 4)}`; return new ValidationError(message); }); } diff --git a/tests/lib/workflow-validator.spec.ts b/tests/lib/workflow-validator.spec.ts index 90922334..f602b865 100644 --- a/tests/lib/workflow-validator.spec.ts +++ b/tests/lib/workflow-validator.spec.ts @@ -16,30 +16,33 @@ */ import { ValidationError, WorkflowValidator } from '../../src'; import { Workflow } from '../../src/lib/definitions/workflow'; +import * as fs from 'fs'; -const validWorkflow = { - id: 'helloworld', - version: '1.0', - specVersion: '0.8', - name: 'Hello World Workflow', - description: 'Inject Hello World', - start: 'Hello State', - states: [ - { - name: 'Hello State', - type: 'inject', - data: { - result: 'Hello World!', +describe('workflow-validator, invalid state', () => { + const validWorkflow = { + id: 'helloworld', + version: '1.0', + specVersion: '0.8', + name: 'Hello World Workflow', + description: 'Inject Hello World', + start: 'Hello State', + states: [ + { + name: 'Hello State', + type: 'inject', + data: { + result: 'Hello World!', + }, + end: true, }, - end: true, - }, - ], -}; + ], + }; -describe('workflow-validator, invalid state', () => { it('should return errors instance of ValidationError if the workflow provided is not valid', () => { + const workflowWithEmptyStates = Workflow.fromSource(JSON.stringify(validWorkflow)); + // @ts-ignore - const workflowWithEmptyStates = Object.assign({ ...validWorkflow }, { states: [] }) as Workflow; + workflowWithEmptyStates.states = []; const workflowValidator = new WorkflowValidator(workflowWithEmptyStates); @@ -49,8 +52,8 @@ describe('workflow-validator, invalid state', () => { }); it('should check if specVersion match the supported sdk version', () => { - // @ts-ignore - const workflowWithInvalidSpecVersion = Object.assign({ ...validWorkflow }, { specVersion: '0.1' }) as Workflow; + const workflowWithInvalidSpecVersion = Workflow.fromSource(JSON.stringify(validWorkflow)); + workflowWithInvalidSpecVersion.specVersion = '0.1'; const workflowValidator = new WorkflowValidator(workflowWithInvalidSpecVersion); @@ -70,12 +73,18 @@ describe('workflow-validator, invalid state', () => { }); describe('workflow-validator, valid state', () => { - it('should have no errors if the workflow is valid', () => { - // @ts-ignore - const workflow = validWorkflow as Workflow; + it('should have no errors', () => { + const testFolder = './tests/examples/'; - const workflowValidator = new WorkflowValidator(workflow); - expect(workflowValidator.errors.length).toBe(0); - expect(workflowValidator.isValid); + const jsonFiles = fs.readdirSync(testFolder).filter((f) => f.endsWith('.json')); + + expect(jsonFiles.length).toBe(9); + + jsonFiles.forEach((f) => { + const file = testFolder + f; + const workflow = Workflow.fromSource(fs.readFileSync(file, 'utf8')); + const workflowValidator = new WorkflowValidator(workflow); + expect(workflowValidator.errors.length).toBe(0); + }); }); });