|
| 1 | +import { Definition, DefinitionWalker, Step } from 'sequential-workflow-model'; |
| 2 | +import { DefinitionModel, PropertyModel, PropertyModels, ValidationError, ValidationResult, createValidationSingleError } from '../model'; |
| 3 | +import { DefinitionContext, ValueContext } from '../context'; |
| 4 | +import { CustomValidatorContext } from './custom-validator-context'; |
| 5 | +import { Path } from '../core'; |
| 6 | + |
| 7 | +export class DefinitionValidator { |
| 8 | + public static create(definitionModel: DefinitionModel, definitionWalker: DefinitionWalker): DefinitionValidator { |
| 9 | + return new DefinitionValidator(definitionModel, definitionWalker); |
| 10 | + } |
| 11 | + |
| 12 | + private constructor(private readonly model: DefinitionModel, private readonly walker: DefinitionWalker) {} |
| 13 | + |
| 14 | + /** |
| 15 | + * Deeply validates the given definition. |
| 16 | + * @param definition The definition to validate. |
| 17 | + * @returns `null` if the definition is valid, otherwise an object describing the validation error. |
| 18 | + */ |
| 19 | + public validate(definition: Definition): DefinitionValidationError | null { |
| 20 | + const rootError = this.validateRoot(definition); |
| 21 | + if (rootError) { |
| 22 | + return { |
| 23 | + ...rootError, |
| 24 | + stepId: null |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + let result: DefinitionValidationError | null = null; |
| 29 | + this.walker.forEach(definition, step => { |
| 30 | + const stepError = this.validateStep(step, definition); |
| 31 | + if (stepError) { |
| 32 | + result = { |
| 33 | + ...stepError, |
| 34 | + stepId: step.id |
| 35 | + }; |
| 36 | + return false; // stop walking |
| 37 | + } |
| 38 | + }); |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + public validateStep(step: Step, definition: Definition): PropertyValidationError | null { |
| 43 | + const definitionContext = DefinitionContext.createForStep(step, definition, this.model, this.walker); |
| 44 | + |
| 45 | + const stepModel = this.model.steps[step.type]; |
| 46 | + if (!stepModel) { |
| 47 | + throw new Error(`Cannot find model for step type: ${step.type}`); |
| 48 | + } |
| 49 | + |
| 50 | + const nameError = this.validateProperty(stepModel.name, definitionContext); |
| 51 | + if (nameError) { |
| 52 | + return { |
| 53 | + propertyPath: stepModel.name.path, |
| 54 | + error: nameError |
| 55 | + }; |
| 56 | + } |
| 57 | + return this.validateProperties(stepModel.properties, definitionContext); |
| 58 | + } |
| 59 | + |
| 60 | + public validateRoot(definition: Definition): PropertyValidationError | null { |
| 61 | + const definitionContext = DefinitionContext.createForRoot(definition, this.model, this.walker); |
| 62 | + return this.validateProperties(this.model.root.properties, definitionContext); |
| 63 | + } |
| 64 | + |
| 65 | + private validateProperties(properties: PropertyModels, definitionContext: DefinitionContext): PropertyValidationError | null { |
| 66 | + for (const propertyName of properties) { |
| 67 | + const error = this.validateProperty(propertyName, definitionContext); |
| 68 | + if (error) { |
| 69 | + return { |
| 70 | + propertyPath: propertyName.path, |
| 71 | + error |
| 72 | + }; |
| 73 | + } |
| 74 | + } |
| 75 | + return null; |
| 76 | + } |
| 77 | + |
| 78 | + private validateProperty(propertyModel: PropertyModel, definitionContext: DefinitionContext): ValidationResult { |
| 79 | + const valueContext = ValueContext.create(propertyModel.value, propertyModel, definitionContext); |
| 80 | + const valueError = propertyModel.value.validate(valueContext); |
| 81 | + if (valueError) { |
| 82 | + return valueError; |
| 83 | + } |
| 84 | + |
| 85 | + if (propertyModel.customValidator) { |
| 86 | + const customContext = CustomValidatorContext.create(propertyModel, definitionContext); |
| 87 | + const customError = propertyModel.customValidator.validate(customContext); |
| 88 | + if (customError) { |
| 89 | + return createValidationSingleError(customError); |
| 90 | + } |
| 91 | + } |
| 92 | + return null; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +export interface PropertyValidationError { |
| 97 | + propertyPath: Path; |
| 98 | + error: ValidationError; |
| 99 | +} |
| 100 | + |
| 101 | +export interface DefinitionValidationError extends PropertyValidationError { |
| 102 | + /** |
| 103 | + * Step id. If it is `null` then the error is related to the root. |
| 104 | + */ |
| 105 | + stepId: string | null; |
| 106 | +} |
0 commit comments