Skip to content

Commit 14345dc

Browse files
Merge pull request #360 from ricardozanini/issue-212
Fix #212 - Add validation for actions in ForEachState
2 parents efd800b + 8fb3f9f commit 14345dc

File tree

2 files changed

+81
-35
lines changed

2 files changed

+81
-35
lines changed

validation/src/main/java/io/serverlessworkflow/validation/WorkflowValidatorImpl.java

+46-34
Original file line numberDiff line numberDiff line change
@@ -144,40 +144,7 @@ public List<ValidationError> validate() {
144144

145145
if (s instanceof OperationState) {
146146
OperationState operationState = (OperationState) s;
147-
148-
List<Action> actions = operationState.getActions();
149-
for (Action action : actions) {
150-
if (action.getFunctionRef() != null) {
151-
if (action.getFunctionRef().getRefName().isEmpty()) {
152-
addValidationError(
153-
"Operation State action functionRef should not be null or empty",
154-
ValidationError.WORKFLOW_VALIDATION);
155-
}
156-
157-
if (!haveFunctionDefinition(
158-
action.getFunctionRef().getRefName(), functions)) {
159-
addValidationError(
160-
"Operation State action functionRef does not reference an existing workflow function definition",
161-
ValidationError.WORKFLOW_VALIDATION);
162-
}
163-
}
164-
165-
if (action.getEventRef() != null) {
166-
167-
if (!haveEventsDefinition(
168-
action.getEventRef().getTriggerEventRef(), events)) {
169-
addValidationError(
170-
"Operation State action trigger event def does not reference an existing workflow event definition",
171-
ValidationError.WORKFLOW_VALIDATION);
172-
}
173-
174-
if (!haveEventsDefinition(action.getEventRef().getResultEventRef(), events)) {
175-
addValidationError(
176-
"Operation State action results event def does not reference an existing workflow event definition",
177-
ValidationError.WORKFLOW_VALIDATION);
178-
}
179-
}
180-
}
147+
checkActionsDefinition(operationState.getActions(), functions, events);
181148
}
182149

183150
if (s instanceof EventState) {
@@ -281,6 +248,7 @@ public List<ValidationError> validate() {
281248

282249
if (s instanceof ForEachState) {
283250
ForEachState forEachState = (ForEachState) s;
251+
checkActionsDefinition(forEachState.getActions(), functions, events);
284252
if (forEachState.getInputCollection() == null
285253
|| forEachState.getInputCollection().isEmpty()) {
286254
addValidationError(
@@ -334,6 +302,50 @@ public WorkflowValidator reset() {
334302
return this;
335303
}
336304

305+
private void checkActionsDefinition(
306+
List<Action> actions, List<FunctionDefinition> functions, List<EventDefinition> events) {
307+
if (actions == null) {
308+
return;
309+
}
310+
for (Action action : actions) {
311+
if (action.getFunctionRef() != null) {
312+
if (action.getFunctionRef().getRefName().isEmpty()) {
313+
addValidationError(
314+
String.format(
315+
"State action '%s' functionRef should not be null or empty", action.getName()),
316+
ValidationError.WORKFLOW_VALIDATION);
317+
}
318+
319+
if (!haveFunctionDefinition(action.getFunctionRef().getRefName(), functions)) {
320+
addValidationError(
321+
String.format(
322+
"State action '%s' functionRef does not reference an existing workflow function definition",
323+
action.getName()),
324+
ValidationError.WORKFLOW_VALIDATION);
325+
}
326+
}
327+
328+
if (action.getEventRef() != null) {
329+
330+
if (!haveEventsDefinition(action.getEventRef().getTriggerEventRef(), events)) {
331+
addValidationError(
332+
String.format(
333+
"State action '%s' trigger event def does not reference an existing workflow event definition",
334+
action.getName()),
335+
ValidationError.WORKFLOW_VALIDATION);
336+
}
337+
338+
if (!haveEventsDefinition(action.getEventRef().getResultEventRef(), events)) {
339+
addValidationError(
340+
String.format(
341+
"State action '%s' results event def does not reference an existing workflow event definition",
342+
action.getName()),
343+
ValidationError.WORKFLOW_VALIDATION);
344+
}
345+
}
346+
}
347+
}
348+
337349
private boolean haveFunctionDefinition(String functionName, List<FunctionDefinition> functions) {
338350
if (functions != null) {
339351
FunctionDefinition fun =

validation/src/test/java/io/serverlessworkflow/validation/test/WorkflowValidationTest.java

+35-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.serverlessworkflow.api.interfaces.WorkflowValidator;
3030
import io.serverlessworkflow.api.retry.RetryDefinition;
3131
import io.serverlessworkflow.api.start.Start;
32+
import io.serverlessworkflow.api.states.ForEachState;
3233
import io.serverlessworkflow.api.states.OperationState;
3334
import io.serverlessworkflow.api.states.SleepState;
3435
import io.serverlessworkflow.api.validation.ValidationError;
@@ -173,7 +174,7 @@ public void testOperationStateNoFunctionRef() {
173174
Assertions.assertEquals(1, validationErrors.size());
174175

175176
Assertions.assertEquals(
176-
"Operation State action functionRef does not reference an existing workflow function definition",
177+
"State action 'null' functionRef does not reference an existing workflow function definition",
177178
validationErrors.get(0).getMessage());
178179
}
179180

@@ -333,4 +334,37 @@ void testEventCall() {
333334
.withEnd(new End())));
334335
Assertions.assertTrue(new WorkflowValidatorImpl().setWorkflow(workflow).validate().isEmpty());
335336
}
337+
338+
/**
339+
* @see <a href="https://github.com/serverlessworkflow/sdk-java/issues/212">Validation missing out
340+
* on refname in foreach>actions</a>
341+
*/
342+
@Test
343+
void testActionDefForEach() {
344+
Workflow workflow =
345+
new Workflow()
346+
.withId("test-workflow")
347+
.withVersion("1.0")
348+
.withStart(new Start().withStateName("TestingForEach"))
349+
.withFunctions(new Functions(Arrays.asList(new FunctionDefinition("Test"))))
350+
.withStates(
351+
Arrays.asList(
352+
new ForEachState()
353+
.withName("TestingForEach")
354+
.withInputCollection("${ .archives }")
355+
.withIterationParam("archive")
356+
.withOutputCollection("${ .output}")
357+
.withActions(
358+
Arrays.asList(
359+
new Action()
360+
.withName("callFn")
361+
.withFunctionRef(new FunctionRef("DoesNotExist"))))
362+
.withEnd(new End())));
363+
final List<ValidationError> validationErrors =
364+
new WorkflowValidatorImpl().setWorkflow(workflow).validate();
365+
Assertions.assertEquals(1, validationErrors.size());
366+
Assertions.assertEquals(
367+
"State action 'callFn' functionRef does not reference an existing workflow function definition",
368+
validationErrors.get(0).getMessage());
369+
}
336370
}

0 commit comments

Comments
 (0)