Skip to content

Commit 86f11ee

Browse files
committed
moved validate to constructor and added isValid property
Signed-off-by: Antonio Mendoza Pérez <antmendoza@gmail.com>
1 parent caf026d commit 86f11ee

File tree

4 files changed

+17
-18
lines changed

4 files changed

+17
-18
lines changed

src/lib/validation-error.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class ValidationError {
2+
constructor(readonly message: string) {}
3+
}

src/lib/workflow-validator.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,33 @@
1818
import { ValidateFunction } 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 */
2425
errors: ValidationError[] | never[] = [];
25-
/** The validate function */
26-
private validateFn: ValidateFunction<Specification.Workflow>;
26+
27+
/** Whether the workflow is valid or not */
28+
isValid: boolean;
2729

2830
/**
2931
* Creates a new WorkflowValidator for the provided workflow
3032
* @param {Workflow} workflow The workflow to validate
3133
*/
3234
constructor(private workflow: Specification.Workflow) {
33-
this.validateFn = validators.get('Workflow') as ValidateFunction<Specification.Workflow>;
35+
this.validate();
3436
}
3537

3638
/**
3739
* Validates the workflow, populates the errors if any
38-
* @returns {boolean} If the workflow is valid or not
3940
*/
40-
validate(): boolean {
41-
const isValid = this.validateFn(this.workflow);
42-
if (this.validateFn.errors) {
43-
this.errors = this.validateFn.errors.map(
41+
private validate(): void {
42+
const validateFn = validators.get('Workflow') as ValidateFunction<Specification.Workflow>;
43+
this.isValid = validateFn(this.workflow);
44+
if (validateFn.errors) {
45+
this.errors = validateFn.errors.map(
4446
(error) => new ValidationError(`invalid: ${error.instancePath} | ${error.schemaPath} | ${error.message}`)
4547
);
4648
}
47-
return isValid;
4849
}
4950
}
50-
51-
export class ValidationError {
52-
constructor(readonly message: string) {}
53-
}

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: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@ describe('workflow-validator', () => {
3030
} as Workflow;
3131

3232
const workflowValidator = new WorkflowValidator(workflow);
33-
expect(workflowValidator.errors.length).toBe(0);
34-
35-
workflowValidator.validate();
33+
expect(workflowValidator.isValid).toBeFalsy('Expected isValid to be false');
3634
expect(workflowValidator.errors.length).toBe(1);
3735
expect(workflowValidator.errors[0].constructor === ValidationError).toBeTruthy(
3836
'Expected errors to be instance of ValidationError'
@@ -57,9 +55,9 @@ describe('workflow-validator', () => {
5755
},
5856
],
5957
} as Workflow;
60-
58+
6159
const workflowValidator = new WorkflowValidator(workflow);
62-
workflowValidator.validate();
6360
expect(workflowValidator.errors.length).toBe(0);
61+
expect(workflowValidator.isValid).toBeTruthy('Expected isValid to be true');
6462
});
6563
});

0 commit comments

Comments
 (0)