Skip to content

Commit

Permalink
Fix workflow validator (#163)
Browse files Browse the repository at this point in the history
fix WorkflowValidator
  • Loading branch information
antmendoza authored Jan 11, 2022
1 parent 2636805 commit 6d4dfb5
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 50 deletions.
43 changes: 22 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
```

Expand Down
6 changes: 4 additions & 2 deletions src/lib/workflow-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export class WorkflowValidator {
*/
constructor(private workflow: Specification.Workflow) {
const validateFn = validators.get('Workflow') as ValidateFunction<Specification.Workflow>;
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);
});
}
Expand Down
63 changes: 36 additions & 27 deletions tests/lib/workflow-validator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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);
});
});
});

0 comments on commit 6d4dfb5

Please sign in to comment.