Skip to content

Commit e376d7a

Browse files
authored
Merge pull request #134 from antmendoza/verifySpecVersion
verify specVersion property match the sdk supported spec version
2 parents 1db6ea7 + 45972ba commit e376d7a

File tree

3 files changed

+66
-45
lines changed

3 files changed

+66
-45
lines changed

src/lib/validation-error.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { DefinedError } from 'ajv';
18-
1917
export class ValidationError {
20-
readonly message: string;
21-
22-
constructor(readonly error: DefinedError) {
23-
this.message = `invalid: ${error.instancePath} | ${error.schemaPath} | ${error.message}`;
24-
}
18+
constructor(public readonly message: string) {}
2519
}

src/lib/workflow-validator.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { ValidateFunction, DefinedError } from 'ajv';
17+
import { ValidateFunction } from 'ajv';
1818
import { Specification } from './definitions';
1919
import { validators } from './validators';
2020
import { ValidationError } from './validation-error';
2121

22+
import { schemaVersion } from '../../package.json';
23+
2224
export class WorkflowValidator {
2325
/** The validation errors after running validate(), if any */
24-
readonly errors: ValidationError[] | never[] = [];
26+
readonly errors: ValidationError[] = [];
2527

2628
/** Whether the workflow is valid or not */
2729
readonly isValid: boolean;
@@ -32,9 +34,20 @@ export class WorkflowValidator {
3234
*/
3335
constructor(private workflow: Specification.Workflow) {
3436
const validateFn = validators.get('Workflow') as ValidateFunction<Specification.Workflow>;
35-
this.isValid = validateFn(this.workflow);
37+
validateFn(this.workflow);
3638
if (validateFn.errors) {
37-
this.errors = validateFn.errors.map((error) => new ValidationError(error as DefinedError));
39+
this.errors = validateFn.errors.map((error) => {
40+
const message = `invalid: ${error.instancePath} | ${error.schemaPath} | ${error.message}`;
41+
return new ValidationError(message);
42+
});
43+
}
44+
45+
const specVersion = workflow.specVersion;
46+
if (schemaVersion !== specVersion) {
47+
const message = `provided workflow.specVersion value '${specVersion}' can not be different from the SDK supported version '${schemaVersion}'`;
48+
this.errors.push(new ValidationError(message));
3849
}
50+
51+
this.isValid = this.errors.length === 0;
3952
}
4053
}

tests/lib/workflow-validator.spec.ts

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,48 +17,62 @@
1717
import { ValidationError, WorkflowValidator } from '../../src';
1818
import { Workflow } from '../../src/lib/definitions/workflow';
1919

20-
describe('workflow-validator', () => {
20+
const validWorkflow = {
21+
id: 'helloworld',
22+
version: '1.0',
23+
specVersion: '0.7',
24+
name: 'Hello World Workflow',
25+
description: 'Inject Hello World',
26+
start: 'Hello State',
27+
states: [
28+
{
29+
name: 'Hello State',
30+
type: 'inject',
31+
data: {
32+
result: 'Hello World!',
33+
},
34+
end: true,
35+
},
36+
],
37+
};
38+
39+
describe('workflow-validator, invalid state', () => {
2140
it('should return errors instance of ValidationError if the workflow provided is not valid', () => {
2241
// @ts-ignore
23-
const workflow = {
24-
id: 'helloworld',
25-
name: 'Hello World Workflow',
26-
version: '1.0',
27-
specVersion: '0.7',
28-
description: 'Inject Hello World',
29-
start: 'Hello State',
30-
states: [],
31-
} as Workflow;
42+
const workflowWithEmptyStates = Object.assign({ ...validWorkflow }, { states: [] }) as Workflow;
3243

33-
const workflowValidator = new WorkflowValidator(workflow);
34-
expect(workflowValidator.isValid).toBeFalsy('Expected isValid to be false');
35-
expect(workflowValidator.errors.length).toBe(1);
36-
expect(workflowValidator.errors[0].constructor === ValidationError).toBeTruthy(
37-
'Expected errors to be instance of ValidationError'
38-
);
44+
const workflowValidator = new WorkflowValidator(workflowWithEmptyStates);
45+
46+
const numErrors = 1;
47+
expectInvalidWorkflow(workflowValidator, numErrors);
3948
expect(workflowValidator.errors[0].message).toMatch('states');
4049
});
4150

51+
it('should check if specVersion match the supported sdk version', () => {
52+
// @ts-ignore
53+
const workflowWithInvalidSpecVersion = Object.assign({ ...validWorkflow }, { specVersion: '0.1' }) as Workflow;
54+
55+
const workflowValidator = new WorkflowValidator(workflowWithInvalidSpecVersion);
56+
57+
const numErrors = 1;
58+
expectInvalidWorkflow(workflowValidator, numErrors);
59+
60+
expect(workflowValidator.errors[0].message).toMatch('specVersion');
61+
});
62+
63+
function expectInvalidWorkflow(workflowValidator: WorkflowValidator, numErrors: number) {
64+
expect(workflowValidator.isValid).toBeFalsy('Expected isValid to be false');
65+
expect(workflowValidator.errors.length).toBe(numErrors);
66+
workflowValidator.errors.forEach((error) => {
67+
expect(error.constructor === ValidationError).toBeTruthy('Expected errors to be instance of ValidationError');
68+
});
69+
}
70+
});
71+
72+
describe('workflow-validator, valid state', () => {
4273
it('should have no errors if the workflow is valid', () => {
4374
// @ts-ignore
44-
const workflow = {
45-
id: 'helloworld',
46-
version: '1.0',
47-
specVersion: '0.7',
48-
name: 'Hello World Workflow',
49-
description: 'Inject Hello World',
50-
start: 'Hello State',
51-
states: [
52-
{
53-
name: 'Hello State',
54-
type: 'inject',
55-
data: {
56-
result: 'Hello World!',
57-
},
58-
end: true,
59-
},
60-
],
61-
} as Workflow;
75+
const workflow = validWorkflow as Workflow;
6276

6377
const workflowValidator = new WorkflowValidator(workflow);
6478
expect(workflowValidator.errors.length).toBe(0);

0 commit comments

Comments
 (0)