Skip to content

Commit c6e7a75

Browse files
committed
Review comments
Signed-off-by: Francisco Javier Tirado Sarti <[email protected]>
1 parent c7433cb commit c6e7a75

File tree

2 files changed

+67
-25
lines changed

2 files changed

+67
-25
lines changed

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

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ public WorkflowValidator setSource(String source) {
6464
@Override
6565
public Collection<ValidationError> validate() {
6666
validationErrors.clear();
67-
if (workflow == null && schemaValidationEnabled && source != null) {
67+
if (workflow == null) {
6868
try {
69-
70-
JsonSchemaFactory.getInstance(VersionFlag.V202012)
71-
.getSchema(workflowSchema)
72-
.validate(Utils.getNode(source))
73-
.forEach(m -> addValidationError(m.getMessage(), ValidationError.SCHEMA_VALIDATION));
69+
if (schemaValidationEnabled && source != null) {
70+
JsonSchemaFactory.getInstance(VersionFlag.V7)
71+
.getSchema(workflowSchema)
72+
.validate(Utils.getNode(source))
73+
.forEach(m -> addValidationError(m.getMessage(), ValidationError.SCHEMA_VALIDATION));
74+
}
7475
} catch (IOException e) {
7576
logger.error("Unexpected error during validation", e);
7677
}
@@ -163,17 +164,6 @@ public Collection<ValidationError> validate() {
163164
}
164165

165166
if (action.getEventRef() != null) {
166-
if (action.getEventRef().getTriggerEventRef().isEmpty()) {
167-
addValidationError(
168-
"Operation State action trigger eventRef does not reference an existing workflow event definition",
169-
ValidationError.WORKFLOW_VALIDATION);
170-
}
171-
172-
if (action.getEventRef().getResultEventRef().isEmpty()) {
173-
addValidationError(
174-
"Operation State action results eventRef does not reference an existing workflow event definition",
175-
ValidationError.WORKFLOW_VALIDATION);
176-
}
177167

178168
if (!haveEventsDefinition(
179169
action.getEventRef().getTriggerEventRef(), events)) {
@@ -357,22 +347,19 @@ private boolean haveFunctionDefinition(String functionName, List<FunctionDefinit
357347
}
358348

359349
private boolean haveEventsDefinition(String eventName, List<EventDefinition> events) {
350+
if (eventName == null) {
351+
return true;
352+
}
360353
if (events != null) {
361354
EventDefinition eve =
362355
events.stream().filter(e -> e.getName().equals(eventName)).findFirst().orElse(null);
363-
364356
return eve == null ? false : true;
365357
} else {
366358
return false;
367359
}
368360
}
369361

370-
private static final Set<String> skipMessages =
371-
Set.of(
372-
"$.start: string found, object expected",
373-
"$.functions: array found, object expected",
374-
"$.events: array found, object expected",
375-
"$.retries: array found, object expected");
362+
private static final Set<String> skipMessages = Set.of("$.start: string found, object expected");
376363

377364
private void addValidationError(String message, String type) {
378365
if (skipMessages.contains(message)) {
@@ -385,7 +372,6 @@ private void addValidationError(String message, String type) {
385372
}
386373

387374
private class Validation {
388-
389375
final Set<String> states = new HashSet<>();
390376
Integer endStates = 0;
391377

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,26 @@
1515
*/
1616
package io.serverlessworkflow.validation.test;
1717

18+
import static io.serverlessworkflow.api.states.DefaultState.Type.OPERATION;
1819
import static io.serverlessworkflow.api.states.DefaultState.Type.SLEEP;
1920

2021
import io.serverlessworkflow.api.Workflow;
22+
import io.serverlessworkflow.api.actions.Action;
2123
import io.serverlessworkflow.api.end.End;
24+
import io.serverlessworkflow.api.events.EventDefinition;
25+
import io.serverlessworkflow.api.events.EventRef;
26+
import io.serverlessworkflow.api.functions.FunctionDefinition;
27+
import io.serverlessworkflow.api.functions.FunctionDefinition.Type;
28+
import io.serverlessworkflow.api.functions.FunctionRef;
2229
import io.serverlessworkflow.api.interfaces.WorkflowValidator;
30+
import io.serverlessworkflow.api.retry.RetryDefinition;
2331
import io.serverlessworkflow.api.start.Start;
32+
import io.serverlessworkflow.api.states.OperationState;
2433
import io.serverlessworkflow.api.states.SleepState;
2534
import io.serverlessworkflow.api.validation.ValidationError;
35+
import io.serverlessworkflow.api.workflow.Events;
36+
import io.serverlessworkflow.api.workflow.Functions;
37+
import io.serverlessworkflow.api.workflow.Retries;
2638
import io.serverlessworkflow.validation.WorkflowValidatorImpl;
2739
import java.util.Arrays;
2840
import java.util.Collection;
@@ -123,6 +135,50 @@ public void testWorkflowMissingStatesIdAndKey() {
123135
2);
124136
}
125137

138+
@Test
139+
void testFunctionCall() {
140+
Workflow workflow =
141+
new Workflow()
142+
.withId("test-workflow")
143+
.withVersion("1.0")
144+
.withStart(new Start().withStateName("start"))
145+
.withFunctions(
146+
new Functions(
147+
Arrays.asList(new FunctionDefinition("expression").withType(Type.EXPRESSION))))
148+
.withStates(
149+
Arrays.asList(
150+
new OperationState()
151+
.withName("start")
152+
.withType(OPERATION)
153+
.withActions(
154+
Arrays.asList(
155+
new Action().withFunctionRef(new FunctionRef("expression"))))
156+
.withEnd(new End())));
157+
Assertions.assertTrue(new WorkflowValidatorImpl().setWorkflow(workflow).validate().isEmpty());
158+
}
159+
160+
@Test
161+
void testEventCall() {
162+
Workflow workflow =
163+
new Workflow()
164+
.withId("test-workflow")
165+
.withVersion("1.0")
166+
.withStart(new Start().withStateName("start"))
167+
.withEvents(new Events(Arrays.asList(new EventDefinition().withName("event"))))
168+
.withRetries(new Retries(Arrays.asList(new RetryDefinition("start", "PT1S"))))
169+
.withStates(
170+
Arrays.asList(
171+
new OperationState()
172+
.withName("start")
173+
.withType(OPERATION)
174+
.withActions(
175+
Arrays.asList(
176+
new Action()
177+
.withEventRef(new EventRef().withTriggerEventRef("event"))))
178+
.withEnd(new End())));
179+
Assertions.assertTrue(new WorkflowValidatorImpl().setWorkflow(workflow).validate().isEmpty());
180+
}
181+
126182
@Test
127183
public void testOperationStateNoFunctionRef() {
128184
WorkflowValidator workflowValidator = new WorkflowValidatorImpl();

0 commit comments

Comments
 (0)