Skip to content

Commit b24ccad

Browse files
authored
Merge pull request #112 from antmendoza/add_validationError_class
validate method return proprietary class
2 parents cf71389 + c2c45fb commit b24ccad

File tree

4 files changed

+104
-14
lines changed

4 files changed

+104
-14
lines changed

src/lib/validation-error.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* oUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
import { DefinedError } from 'ajv';
19+
20+
export class ValidationError {
21+
readonly message: string;
22+
23+
constructor(readonly error: DefinedError) {
24+
this.message = `invalid: ${error.instancePath} | ${error.schemaPath} | ${error.message}`;
25+
}
26+
}

src/lib/workflow-validator.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,27 @@
1515
*
1616
*/
1717

18-
import { DefinedError, ValidateFunction } from 'ajv';
18+
import { ValidateFunction, DefinedError } from 'ajv';
1919
import { Specification } from './definitions';
2020
import { validators } from './validators';
21+
import { ValidationError } from './validation-error';
2122

2223
export class WorkflowValidator {
2324
/** The validation errors after running validate(), if any */
24-
errors: DefinedError[] | never[] = [];
25-
/** The validate function */
26-
private validateFn: ValidateFunction<Specification.Workflow>;
25+
readonly errors: ValidationError[] | never[] = [];
26+
27+
/** Whether the workflow is valid or not */
28+
readonly isValid: boolean;
29+
2730
/**
2831
* Creates a new WorkflowValidator for the provided workflow
2932
* @param {Workflow} workflow The workflow to validate
3033
*/
3134
constructor(private workflow: Specification.Workflow) {
32-
this.validateFn = validators.get('Workflow') as ValidateFunction<Specification.Workflow>;
33-
}
34-
/**
35-
* Validates the workflow, populates the errors if any
36-
* @returns {boolean} If the workflow is valid or not
37-
*/
38-
validate(): boolean {
39-
const isValid = this.validateFn(this.workflow);
40-
this.errors = this.validateFn.errors as DefinedError[];
41-
return isValid;
35+
const validateFn = validators.get('Workflow') as ValidateFunction<Specification.Workflow>;
36+
this.isValid = validateFn(this.workflow);
37+
if (validateFn.errors) {
38+
this.errors = validateFn.errors.map((error) => new ValidationError(error as DefinedError));
39+
}
4240
}
4341
}

src/serverless-workflow-sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*
1616
*/
1717

18+
export * from './lib/validation-error';
1819
export * from './lib/workflow-converter';
1920
export * from './lib/workflow-validator';
2021
export * from './lib/validators';

tests/workflow-validator.spec.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2021-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
import { ValidationError, WorkflowValidator } from '../src/';
18+
import { Workflow } from '../src/lib/definitions/workflow';
19+
20+
describe('workflow-validator', () => {
21+
it('should return errors instance of ValidationError if the workflow provided is not valid', () => {
22+
// @ts-ignore
23+
const workflow = {
24+
id: 'helloworld',
25+
name: 'Hello World Workflow',
26+
version: '1.0',
27+
description: 'Inject Hello World',
28+
start: 'Hello State',
29+
states: [],
30+
} as Workflow;
31+
32+
const workflowValidator = new WorkflowValidator(workflow);
33+
expect(workflowValidator.isValid).toBeFalsy('Expected isValid to be false');
34+
expect(workflowValidator.errors.length).toBe(1);
35+
expect(workflowValidator.errors[0].constructor === ValidationError).toBeTruthy(
36+
'Expected errors to be instance of ValidationError'
37+
);
38+
expect(workflowValidator.errors[0].message).toMatch("states");
39+
40+
});
41+
42+
it('should have no errors if the workflow is valid', () => {
43+
const workflow = {
44+
id: 'helloworld',
45+
version: '1.0',
46+
name: 'Hello World Workflow',
47+
description: 'Inject Hello World',
48+
start: 'Hello State',
49+
states: [
50+
{
51+
name: 'Hello State',
52+
type: 'inject',
53+
data: {
54+
result: 'Hello World!',
55+
},
56+
end: true,
57+
},
58+
],
59+
} as Workflow;
60+
61+
const workflowValidator = new WorkflowValidator(workflow);
62+
expect(workflowValidator.errors.length).toBe(0);
63+
expect(workflowValidator.isValid).toBeTruthy('Expected isValid to be true');
64+
});
65+
});

0 commit comments

Comments
 (0)