25
25
import io .serverlessworkflow .api .functions .FunctionDefinition ;
26
26
import io .serverlessworkflow .api .interfaces .State ;
27
27
import io .serverlessworkflow .api .interfaces .WorkflowValidator ;
28
+ import io .serverlessworkflow .api .retry .RetryDefinition ;
28
29
import io .serverlessworkflow .api .states .*;
29
30
import io .serverlessworkflow .api .switchconditions .DataCondition ;
30
31
import io .serverlessworkflow .api .switchconditions .EventCondition ;
@@ -78,7 +79,7 @@ public List<ValidationError> validate() {
78
79
79
80
// if there are schema validation errors
80
81
// there is no point of doing the workflow validation
81
- if (validationErrors .size () > 0 ) {
82
+ if (! validationErrors .isEmpty () ) {
82
83
return validationErrors ;
83
84
} else if (workflow == null ) {
84
85
workflow = Workflow .fromSource (source );
@@ -101,6 +102,19 @@ public List<ValidationError> validate() {
101
102
"Workflow version should not be empty" , ValidationError .WORKFLOW_VALIDATION );
102
103
}
103
104
105
+ if (workflow .getRetries () != null && workflow .getRetries ().getRetryDefs () != null ) {
106
+ workflow
107
+ .getRetries ()
108
+ .getRetryDefs ()
109
+ .forEach (
110
+ r -> {
111
+ if (r .getName () == null || r .getName ().isEmpty ()) {
112
+ addValidationError (
113
+ "Retry name should not be empty" , ValidationError .WORKFLOW_VALIDATION );
114
+ }
115
+ });
116
+ }
117
+
104
118
if (workflow .getStates () == null || workflow .getStates ().isEmpty ()) {
105
119
addValidationError ("No states found" , ValidationError .WORKFLOW_VALIDATION );
106
120
}
@@ -149,7 +163,7 @@ public List<ValidationError> validate() {
149
163
150
164
if (s instanceof EventState ) {
151
165
EventState eventState = (EventState ) s ;
152
- if (eventState .getOnEvents () == null || eventState .getOnEvents ().size () < 1 ) {
166
+ if (eventState .getOnEvents () == null || eventState .getOnEvents ().isEmpty () ) {
153
167
addValidationError (
154
168
"Event State has no eventActions defined" ,
155
169
ValidationError .WORKFLOW_VALIDATION );
@@ -158,13 +172,13 @@ public List<ValidationError> validate() {
158
172
for (OnEvents onEvents : eventsActionsList ) {
159
173
160
174
List <String > eventRefs = onEvents .getEventRefs ();
161
- if (eventRefs == null || eventRefs .size () < 1 ) {
175
+ if (eventRefs == null || eventRefs .isEmpty () ) {
162
176
addValidationError (
163
177
"Event State eventsActions has no event refs" ,
164
178
ValidationError .WORKFLOW_VALIDATION );
165
179
} else {
166
180
for (String eventRef : eventRefs ) {
167
- if (! haveEventsDefinition (eventRef , events )) {
181
+ if (isMissingEventsDefinition (eventRef , events )) {
168
182
addValidationError (
169
183
"Event State eventsActions eventRef does not match a declared workflow event definition" ,
170
184
ValidationError .WORKFLOW_VALIDATION );
@@ -177,9 +191,9 @@ public List<ValidationError> validate() {
177
191
if (s instanceof SwitchState ) {
178
192
SwitchState switchState = (SwitchState ) s ;
179
193
if ((switchState .getDataConditions () == null
180
- || switchState .getDataConditions ().size () < 1 )
194
+ || switchState .getDataConditions ().isEmpty () )
181
195
&& (switchState .getEventConditions () == null
182
- || switchState .getEventConditions ().size () < 1 )) {
196
+ || switchState .getEventConditions ().isEmpty () )) {
183
197
addValidationError (
184
198
"Switch state should define either data or event conditions" ,
185
199
ValidationError .WORKFLOW_VALIDATION );
@@ -192,10 +206,10 @@ public List<ValidationError> validate() {
192
206
}
193
207
194
208
if (switchState .getEventConditions () != null
195
- && switchState .getEventConditions ().size () > 0 ) {
209
+ && ! switchState .getEventConditions ().isEmpty () ) {
196
210
List <EventCondition > eventConditions = switchState .getEventConditions ();
197
211
for (EventCondition ec : eventConditions ) {
198
- if (! haveEventsDefinition (ec .getEventRef (), events )) {
212
+ if (isMissingEventsDefinition (ec .getEventRef (), events )) {
199
213
addValidationError (
200
214
"Switch state event condition eventRef does not reference a defined workflow event" ,
201
215
ValidationError .WORKFLOW_VALIDATION );
@@ -207,7 +221,7 @@ public List<ValidationError> validate() {
207
221
}
208
222
209
223
if (switchState .getDataConditions () != null
210
- && switchState .getDataConditions ().size () > 0 ) {
224
+ && ! switchState .getDataConditions ().isEmpty () ) {
211
225
List <DataCondition > dataConditions = switchState .getDataConditions ();
212
226
for (DataCondition dc : dataConditions ) {
213
227
if (dc .getEnd () != null ) {
@@ -219,7 +233,7 @@ public List<ValidationError> validate() {
219
233
220
234
if (s instanceof SleepState ) {
221
235
SleepState sleepState = (SleepState ) s ;
222
- if (sleepState .getDuration () == null || sleepState .getDuration ().length () < 1 ) {
236
+ if (sleepState .getDuration () == null || sleepState .getDuration ().isEmpty () ) {
223
237
addValidationError (
224
238
"Sleep state should have a non-empty time delay" ,
225
239
ValidationError .WORKFLOW_VALIDATION );
@@ -260,13 +274,13 @@ public List<ValidationError> validate() {
260
274
if (s instanceof CallbackState ) {
261
275
CallbackState callbackState = (CallbackState ) s ;
262
276
263
- if (! haveEventsDefinition (callbackState .getEventRef (), events )) {
277
+ if (isMissingEventsDefinition (callbackState .getEventRef (), events )) {
264
278
addValidationError (
265
279
"CallbackState event ref does not reference a defined workflow event definition" ,
266
280
ValidationError .WORKFLOW_VALIDATION );
267
281
}
268
282
269
- if (! haveFunctionDefinition (
283
+ if (isMissingFunctionDefinition (
270
284
callbackState .getAction ().getFunctionRef ().getRefName (), functions )) {
271
285
addValidationError (
272
286
"CallbackState action function ref does not reference a defined workflow function definition" ,
@@ -316,7 +330,7 @@ private void checkActionsDefinition(
316
330
ValidationError .WORKFLOW_VALIDATION );
317
331
}
318
332
319
- if (! haveFunctionDefinition (action .getFunctionRef ().getRefName (), functions )) {
333
+ if (isMissingFunctionDefinition (action .getFunctionRef ().getRefName (), functions )) {
320
334
addValidationError (
321
335
String .format (
322
336
"State action '%s' functionRef does not reference an existing workflow function definition" ,
@@ -327,51 +341,64 @@ private void checkActionsDefinition(
327
341
328
342
if (action .getEventRef () != null ) {
329
343
330
- if (! haveEventsDefinition (action .getEventRef ().getTriggerEventRef (), events )) {
344
+ if (isMissingEventsDefinition (action .getEventRef ().getTriggerEventRef (), events )) {
331
345
addValidationError (
332
346
String .format (
333
347
"State action '%s' trigger event def does not reference an existing workflow event definition" ,
334
348
action .getName ()),
335
349
ValidationError .WORKFLOW_VALIDATION );
336
350
}
337
351
338
- if (! haveEventsDefinition (action .getEventRef ().getResultEventRef (), events )) {
352
+ if (isMissingEventsDefinition (action .getEventRef ().getResultEventRef (), events )) {
339
353
addValidationError (
340
354
String .format (
341
355
"State action '%s' results event def does not reference an existing workflow event definition" ,
342
356
action .getName ()),
343
357
ValidationError .WORKFLOW_VALIDATION );
344
358
}
345
359
}
360
+
361
+ if (action .getRetryRef () != null
362
+ && isMissingRetryDefinition (action .getRetryRef (), workflow .getRetries ().getRetryDefs ())) {
363
+ addValidationError (
364
+ String .format (
365
+ "Operation State action '%s' retryRef does not reference an existing workflow retry definition" ,
366
+ action .getName ()),
367
+ ValidationError .WORKFLOW_VALIDATION );
368
+ }
346
369
}
347
370
}
348
371
349
- private boolean haveFunctionDefinition (String functionName , List <FunctionDefinition > functions ) {
372
+ private boolean isMissingFunctionDefinition (
373
+ String functionName , List <FunctionDefinition > functions ) {
350
374
if (functions != null ) {
351
- FunctionDefinition fun =
352
- functions .stream ().filter (f -> f .getName ().equals (functionName )).findFirst ().orElse (null );
353
-
354
- return fun == null ? false : true ;
375
+ return !functions .stream ().anyMatch (f -> f .getName ().equals (functionName ));
355
376
} else {
356
- return false ;
377
+ return true ;
357
378
}
358
379
}
359
380
360
- private boolean haveEventsDefinition (String eventName , List <EventDefinition > events ) {
381
+ private boolean isMissingEventsDefinition (String eventName , List <EventDefinition > events ) {
361
382
if (eventName == null ) {
362
- return true ;
383
+ return false ;
363
384
}
364
385
if (events != null ) {
365
- EventDefinition eve =
366
- events .stream ().filter (e -> e .getName ().equals (eventName )).findFirst ().orElse (null );
367
- return eve == null ? false : true ;
386
+ return !events .stream ().anyMatch (e -> e .getName ().equals (eventName ));
368
387
} else {
369
- return false ;
388
+ return true ;
370
389
}
371
390
}
372
391
392
+ private boolean isMissingRetryDefinition (String retryName , List <RetryDefinition > retries ) {
393
+ return retries == null
394
+ || !retries .stream ().anyMatch (f -> f .getName () != null && f .getName ().equals (retryName ));
395
+ }
396
+
373
397
private static final Set <String > skipMessages =
374
- Set .of ("$.start: string found, object expected" , "$.functions: array found, object expected" );
398
+ Set .of (
399
+ "$.start: string found, object expected" ,
400
+ "$.functions: array found, object expected" ,
401
+ "$.retries: array found, object expected" );
375
402
376
403
private void addValidationError (String message , String type ) {
377
404
if (skipMessages .contains (message )) {
0 commit comments