Skip to content

Commit caf026d

Browse files
committed
validate method return proprietary class
Signed-off-by: Antonio Mendoza Pérez <[email protected]>
1 parent cf71389 commit caf026d

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

src/lib/workflow-validator.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,39 @@
1515
*
1616
*/
1717

18-
import { DefinedError, ValidateFunction } from 'ajv';
18+
import { ValidateFunction } from 'ajv';
1919
import { Specification } from './definitions';
2020
import { validators } from './validators';
2121

2222
export class WorkflowValidator {
2323
/** The validation errors after running validate(), if any */
24-
errors: DefinedError[] | never[] = [];
24+
errors: ValidationError[] | never[] = [];
2525
/** The validate function */
2626
private validateFn: ValidateFunction<Specification.Workflow>;
27+
2728
/**
2829
* Creates a new WorkflowValidator for the provided workflow
2930
* @param {Workflow} workflow The workflow to validate
3031
*/
3132
constructor(private workflow: Specification.Workflow) {
3233
this.validateFn = validators.get('Workflow') as ValidateFunction<Specification.Workflow>;
3334
}
35+
3436
/**
3537
* Validates the workflow, populates the errors if any
3638
* @returns {boolean} If the workflow is valid or not
3739
*/
3840
validate(): boolean {
3941
const isValid = this.validateFn(this.workflow);
40-
this.errors = this.validateFn.errors as DefinedError[];
42+
if (this.validateFn.errors) {
43+
this.errors = this.validateFn.errors.map(
44+
(error) => new ValidationError(`invalid: ${error.instancePath} | ${error.schemaPath} | ${error.message}`)
45+
);
46+
}
4147
return isValid;
4248
}
4349
}
50+
51+
export class ValidationError {
52+
constructor(readonly message: string) {}
53+
}

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.errors.length).toBe(0);
34+
35+
workflowValidator.validate();
36+
expect(workflowValidator.errors.length).toBe(1);
37+
expect(workflowValidator.errors[0].constructor === ValidationError).toBeTruthy(
38+
'Expected errors to be instance of ValidationError'
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+
workflowValidator.validate();
63+
expect(workflowValidator.errors.length).toBe(0);
64+
});
65+
});

0 commit comments

Comments
 (0)