-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
312 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
import { Definition, DefinitionWalker, Step } from 'sequential-workflow-model'; | ||
import { createDefinitionModel, createRootModel, createStepModel } from '../builders'; | ||
import { numberValueModel } from '../value-models'; | ||
import { DefinitionValidator } from './definition-validator'; | ||
|
||
interface FooDefinition extends Definition { | ||
properties: { | ||
velocity: number; | ||
}; | ||
} | ||
|
||
interface FooStep extends Step { | ||
properties: { | ||
delta: number; | ||
}; | ||
} | ||
|
||
describe('DefinitionValidator', () => { | ||
const model = createDefinitionModel<FooDefinition>(builder => { | ||
builder.root( | ||
createRootModel(root => { | ||
root.property('velocity').value( | ||
numberValueModel({ | ||
min: 0 | ||
}) | ||
); | ||
}) | ||
); | ||
|
||
builder.steps([ | ||
createStepModel<FooStep>('move', 'task', step => { | ||
step.property('delta').value( | ||
numberValueModel({ | ||
max: 0 | ||
}) | ||
); | ||
}) | ||
]); | ||
}); | ||
const walker = new DefinitionWalker(); | ||
const validator = DefinitionValidator.create(model, walker); | ||
|
||
it('returns error when root is invalid', () => { | ||
const def: FooDefinition = { | ||
sequence: [], | ||
properties: { | ||
velocity: -1 // invalid | ||
} | ||
}; | ||
|
||
const error = validator.validate(def); | ||
|
||
expect(error?.stepId).toEqual(null); | ||
expect(error?.propertyPath.toString()).toEqual('properties/velocity'); | ||
expect(error?.error.$).toEqual('The value must be at least 0.'); | ||
}); | ||
|
||
it('returns error when step has invalid delta value', () => { | ||
const def: FooDefinition = { | ||
sequence: [ | ||
{ | ||
type: 'move', | ||
componentType: 'task', | ||
id: '0x000000', | ||
name: 'Correct', | ||
properties: { | ||
delta: -100 | ||
} | ||
}, | ||
{ | ||
type: 'move', | ||
componentType: 'task', | ||
id: '0xFFFFFF', | ||
name: 'Invalid!', | ||
properties: { | ||
delta: 1 // invalid | ||
} | ||
} | ||
], | ||
properties: { | ||
velocity: 100 | ||
} | ||
}; | ||
|
||
const error = validator.validate(def); | ||
|
||
expect(error?.stepId).toEqual('0xFFFFFF'); | ||
expect(error?.propertyPath.toString()).toEqual('properties/delta'); | ||
expect(error?.error.$).toEqual('The value must be at most 0.'); | ||
}); | ||
|
||
it('returns error when step has invalid name', () => { | ||
const def: FooDefinition = { | ||
sequence: [ | ||
{ | ||
type: 'move', | ||
componentType: 'task', | ||
id: '0xAAAAAA', | ||
name: '', // invalid | ||
properties: { | ||
delta: 1 | ||
} | ||
} | ||
], | ||
properties: { | ||
velocity: 100 | ||
} | ||
}; | ||
|
||
const error = validator.validate(def); | ||
|
||
expect(error?.stepId).toEqual('0xAAAAAA'); | ||
expect(error?.propertyPath.toString()).toEqual('name'); | ||
expect(error?.error.$).toEqual('The value must be at least 1 characters long.'); | ||
}); | ||
|
||
it('returns null when definition is valid', () => { | ||
const def: FooDefinition = { | ||
sequence: [ | ||
{ | ||
type: 'move', | ||
componentType: 'task', | ||
id: '0x000000', | ||
name: 'Right', | ||
properties: { | ||
delta: -100 | ||
} | ||
} | ||
], | ||
properties: { | ||
velocity: 100 | ||
} | ||
}; | ||
|
||
const error = validator.validate(def); | ||
|
||
expect(error).toBeNull(); | ||
}); | ||
|
||
it('throws error when step type is not supported', () => { | ||
const def: FooDefinition = { | ||
sequence: [ | ||
{ | ||
type: 'not_supported_type', | ||
componentType: 'task', | ||
id: '0x000000', | ||
name: 'Right', | ||
properties: {} | ||
} | ||
], | ||
properties: { | ||
velocity: 100 | ||
} | ||
}; | ||
|
||
expect(() => validator.validate(def)).toThrowError('Cannot find model for step type: not_supported_type'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import { Definition, DefinitionWalker, Step } from 'sequential-workflow-model'; | ||
import { DefinitionModel, PropertyModel, PropertyModels, ValidationError, ValidationResult, createValidationSingleError } from '../model'; | ||
import { DefinitionContext, ValueContext } from '../context'; | ||
import { CustomValidatorContext } from './custom-validator-context'; | ||
import { Path } from '../core'; | ||
|
||
export class DefinitionValidator { | ||
public static create(definitionModel: DefinitionModel, definitionWalker: DefinitionWalker): DefinitionValidator { | ||
return new DefinitionValidator(definitionModel, definitionWalker); | ||
} | ||
|
||
private constructor(private readonly model: DefinitionModel, private readonly walker: DefinitionWalker) {} | ||
|
||
/** | ||
* Deeply validates the given definition. | ||
* @param definition The definition to validate. | ||
* @returns `null` if the definition is valid, otherwise an object describing the validation error. | ||
*/ | ||
public validate(definition: Definition): DefinitionValidationError | null { | ||
const rootError = this.validateRoot(definition); | ||
if (rootError) { | ||
return { | ||
...rootError, | ||
stepId: null | ||
}; | ||
} | ||
|
||
let result: DefinitionValidationError | null = null; | ||
this.walker.forEach(definition, step => { | ||
const stepError = this.validateStep(step, definition); | ||
if (stepError) { | ||
result = { | ||
...stepError, | ||
stepId: step.id | ||
}; | ||
return false; // stop walking | ||
} | ||
}); | ||
return result; | ||
} | ||
|
||
public validateStep(step: Step, definition: Definition): PropertyValidationError | null { | ||
const definitionContext = DefinitionContext.createForStep(step, definition, this.model, this.walker); | ||
|
||
const stepModel = this.model.steps[step.type]; | ||
if (!stepModel) { | ||
throw new Error(`Cannot find model for step type: ${step.type}`); | ||
} | ||
|
||
const nameError = this.validateProperty(stepModel.name, definitionContext); | ||
if (nameError) { | ||
return { | ||
propertyPath: stepModel.name.path, | ||
error: nameError | ||
}; | ||
} | ||
return this.validateProperties(stepModel.properties, definitionContext); | ||
} | ||
|
||
public validateRoot(definition: Definition): PropertyValidationError | null { | ||
const definitionContext = DefinitionContext.createForRoot(definition, this.model, this.walker); | ||
return this.validateProperties(this.model.root.properties, definitionContext); | ||
} | ||
|
||
private validateProperties(properties: PropertyModels, definitionContext: DefinitionContext): PropertyValidationError | null { | ||
for (const propertyName of properties) { | ||
const error = this.validateProperty(propertyName, definitionContext); | ||
if (error) { | ||
return { | ||
propertyPath: propertyName.path, | ||
error | ||
}; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private validateProperty(propertyModel: PropertyModel, definitionContext: DefinitionContext): ValidationResult { | ||
const valueContext = ValueContext.create(propertyModel.value, propertyModel, definitionContext); | ||
const valueError = propertyModel.value.validate(valueContext); | ||
if (valueError) { | ||
return valueError; | ||
} | ||
|
||
if (propertyModel.customValidator) { | ||
const customContext = CustomValidatorContext.create(propertyModel, definitionContext); | ||
const customError = propertyModel.customValidator.validate(customContext); | ||
if (customError) { | ||
return createValidationSingleError(customError); | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
export interface PropertyValidationError { | ||
propertyPath: Path; | ||
error: ValidationError; | ||
} | ||
|
||
export interface DefinitionValidationError extends PropertyValidationError { | ||
/** | ||
* Step id. If it is `null` then the error is related to the root. | ||
*/ | ||
stepId: string | null; | ||
} |
Oops, something went wrong.