Skip to content

Commit

Permalink
Validate that new directives are within the plan bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Nov 22, 2024
1 parent 7cfbbfc commit 500fa2a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,20 @@ abstract class EasyEditablePlanDriver(
protected abstract fun simulateInternal(options: SimulateOptions)

/**
* Optional validation hook for new activities. The default implementation does nothing.
* Optional validation hook for new activities.
*
* Implementor should throw if the arguments are invalid.
* The default implementation checks if the activity is within the bounds of the plan. The implementor can
* add additional checks by overriding this method and calling `super.validate(directive)`. Implementor
* should throw if the directive is invalid.
*/
protected open fun validateArguments(directive: Directive<AnyDirective>) {}
protected open fun validate(directive: Directive<AnyDirective>) {
if (directive.startTime > duration()) {
throw Exception("New activity with id ${directive.id.id()} would start after the end of the plan")
}
if (directive.start is DirectiveStart.Absolute && directive.startTime.isNegative) {
throw Exception("New activity with id ${directive.id.id()} would start before the beginning of the plan")
}
}

private data class Commit(
val diff: List<Edit>,
Expand Down Expand Up @@ -153,7 +162,7 @@ abstract class EasyEditablePlanDriver(
val resolved = directive.resolve(id, parent)
uncommittedChanges.add(Edit.Create(resolved))

validateArguments(resolved)
validate(resolved)

createInternal(resolved)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ data class InMemoryEditablePlan(
simulationFacade.simulateWithResults(plan, options.pause.resolve(this))
}

override fun validateArguments(directive: Directive<AnyDirective>) {
override fun validate(directive: Directive<AnyDirective>) {
super.validate(directive)
lookupActivityType(directive.type).specType.inputType.validateArguments(directive.inner.arguments)
}

Expand Down

0 comments on commit 500fa2a

Please sign in to comment.