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
}
@@ -154,7 +168,7 @@ public List<ValidationError> validate() {
154
168
ValidationError .WORKFLOW_VALIDATION );
155
169
}
156
170
157
- if (! haveFunctionDefinition (
171
+ if (isMissingFunctionDefinition (
158
172
action .getFunctionRef ().getRefName (), functions )) {
159
173
addValidationError (
160
174
"Operation State action functionRef does not reference an existing workflow function definition" ,
@@ -164,25 +178,36 @@ public List<ValidationError> validate() {
164
178
165
179
if (action .getEventRef () != null ) {
166
180
167
- if (! haveEventsDefinition (
181
+ if (isMissingEventsDefinition (
168
182
action .getEventRef ().getTriggerEventRef (), events )) {
169
183
addValidationError (
170
184
"Operation State action trigger event def does not reference an existing workflow event definition" ,
171
185
ValidationError .WORKFLOW_VALIDATION );
172
186
}
173
187
174
- if (!haveEventsDefinition (action .getEventRef ().getResultEventRef (), events )) {
188
+ if (isMissingEventsDefinition (
189
+ action .getEventRef ().getResultEventRef (), events )) {
175
190
addValidationError (
176
191
"Operation State action results event def does not reference an existing workflow event definition" ,
177
192
ValidationError .WORKFLOW_VALIDATION );
178
193
}
179
194
}
195
+
196
+ if (action .getRetryRef () != null
197
+ && isMissingRetryDefinition (
198
+ action .getRetryRef (), workflow .getRetries ().getRetryDefs ())) {
199
+ addValidationError (
200
+ String .format (
201
+ "Operation State action '%s' retryRef does not reference an existing workflow retry definition" ,
202
+ action .getName ()),
203
+ ValidationError .WORKFLOW_VALIDATION );
204
+ }
180
205
}
181
206
}
182
207
183
208
if (s instanceof EventState ) {
184
209
EventState eventState = (EventState ) s ;
185
- if (eventState .getOnEvents () == null || eventState .getOnEvents ().size () < 1 ) {
210
+ if (eventState .getOnEvents () == null || eventState .getOnEvents ().isEmpty () ) {
186
211
addValidationError (
187
212
"Event State has no eventActions defined" ,
188
213
ValidationError .WORKFLOW_VALIDATION );
@@ -191,13 +216,13 @@ public List<ValidationError> validate() {
191
216
for (OnEvents onEvents : eventsActionsList ) {
192
217
193
218
List <String > eventRefs = onEvents .getEventRefs ();
194
- if (eventRefs == null || eventRefs .size () < 1 ) {
219
+ if (eventRefs == null || eventRefs .isEmpty () ) {
195
220
addValidationError (
196
221
"Event State eventsActions has no event refs" ,
197
222
ValidationError .WORKFLOW_VALIDATION );
198
223
} else {
199
224
for (String eventRef : eventRefs ) {
200
- if (! haveEventsDefinition (eventRef , events )) {
225
+ if (isMissingEventsDefinition (eventRef , events )) {
201
226
addValidationError (
202
227
"Event State eventsActions eventRef does not match a declared workflow event definition" ,
203
228
ValidationError .WORKFLOW_VALIDATION );
@@ -210,9 +235,9 @@ public List<ValidationError> validate() {
210
235
if (s instanceof SwitchState ) {
211
236
SwitchState switchState = (SwitchState ) s ;
212
237
if ((switchState .getDataConditions () == null
213
- || switchState .getDataConditions ().size () < 1 )
238
+ || switchState .getDataConditions ().isEmpty () )
214
239
&& (switchState .getEventConditions () == null
215
- || switchState .getEventConditions ().size () < 1 )) {
240
+ || switchState .getEventConditions ().isEmpty () )) {
216
241
addValidationError (
217
242
"Switch state should define either data or event conditions" ,
218
243
ValidationError .WORKFLOW_VALIDATION );
@@ -225,10 +250,10 @@ public List<ValidationError> validate() {
225
250
}
226
251
227
252
if (switchState .getEventConditions () != null
228
- && switchState .getEventConditions ().size () > 0 ) {
253
+ && ! switchState .getEventConditions ().isEmpty () ) {
229
254
List <EventCondition > eventConditions = switchState .getEventConditions ();
230
255
for (EventCondition ec : eventConditions ) {
231
- if (! haveEventsDefinition (ec .getEventRef (), events )) {
256
+ if (isMissingEventsDefinition (ec .getEventRef (), events )) {
232
257
addValidationError (
233
258
"Switch state event condition eventRef does not reference a defined workflow event" ,
234
259
ValidationError .WORKFLOW_VALIDATION );
@@ -240,7 +265,7 @@ public List<ValidationError> validate() {
240
265
}
241
266
242
267
if (switchState .getDataConditions () != null
243
- && switchState .getDataConditions ().size () > 0 ) {
268
+ && ! switchState .getDataConditions ().isEmpty () ) {
244
269
List <DataCondition > dataConditions = switchState .getDataConditions ();
245
270
for (DataCondition dc : dataConditions ) {
246
271
if (dc .getEnd () != null ) {
@@ -252,7 +277,7 @@ public List<ValidationError> validate() {
252
277
253
278
if (s instanceof SleepState ) {
254
279
SleepState sleepState = (SleepState ) s ;
255
- if (sleepState .getDuration () == null || sleepState .getDuration ().length () < 1 ) {
280
+ if (sleepState .getDuration () == null || sleepState .getDuration ().isEmpty () ) {
256
281
addValidationError (
257
282
"Sleep state should have a non-empty time delay" ,
258
283
ValidationError .WORKFLOW_VALIDATION );
@@ -292,13 +317,13 @@ public List<ValidationError> validate() {
292
317
if (s instanceof CallbackState ) {
293
318
CallbackState callbackState = (CallbackState ) s ;
294
319
295
- if (! haveEventsDefinition (callbackState .getEventRef (), events )) {
320
+ if (isMissingEventsDefinition (callbackState .getEventRef (), events )) {
296
321
addValidationError (
297
322
"CallbackState event ref does not reference a defined workflow event definition" ,
298
323
ValidationError .WORKFLOW_VALIDATION );
299
324
}
300
325
301
- if (! haveFunctionDefinition (
326
+ if (isMissingFunctionDefinition (
302
327
callbackState .getAction ().getFunctionRef ().getRefName (), functions )) {
303
328
addValidationError (
304
329
"CallbackState action function ref does not reference a defined workflow function definition" ,
@@ -334,32 +359,48 @@ public WorkflowValidator reset() {
334
359
return this ;
335
360
}
336
361
337
- private boolean haveFunctionDefinition (String functionName , List <FunctionDefinition > functions ) {
362
+ private boolean isMissingFunctionDefinition (
363
+ String functionName , List <FunctionDefinition > functions ) {
338
364
if (functions != null ) {
339
- FunctionDefinition fun =
340
- functions .stream ().filter (f -> f .getName ().equals (functionName )).findFirst ().orElse (null );
341
-
342
- return fun == null ? false : true ;
365
+ return functions .stream ()
366
+ .filter (f -> f .getName ().equals (functionName ))
367
+ .findFirst ()
368
+ .orElse (null )
369
+ == null ;
343
370
} else {
344
- return false ;
371
+ return true ;
345
372
}
346
373
}
347
374
348
- private boolean haveEventsDefinition (String eventName , List <EventDefinition > events ) {
375
+ private boolean isMissingEventsDefinition (String eventName , List <EventDefinition > events ) {
349
376
if (eventName == null ) {
350
- return true ;
377
+ return false ;
351
378
}
352
379
if (events != null ) {
353
- EventDefinition eve =
354
- events .stream ().filter (e -> e .getName ().equals (eventName )).findFirst ().orElse (null );
355
- return eve == null ? false : true ;
380
+ return events .stream ().filter (e -> e .getName ().equals (eventName )).findFirst ().orElse (null )
381
+ == null ;
356
382
} else {
357
- return false ;
383
+ return true ;
384
+ }
385
+ }
386
+
387
+ private boolean isMissingRetryDefinition (String retryName , List <RetryDefinition > retries ) {
388
+ if (retries != null ) {
389
+ return retries .stream ()
390
+ .filter (f -> f .getName () != null && f .getName ().equals (retryName ))
391
+ .findFirst ()
392
+ .orElse (null )
393
+ == null ;
394
+ } else {
395
+ return true ;
358
396
}
359
397
}
360
398
361
399
private static final Set <String > skipMessages =
362
- Set .of ("$.start: string found, object expected" , "$.functions: array found, object expected" );
400
+ Set .of (
401
+ "$.start: string found, object expected" ,
402
+ "$.functions: array found, object expected" ,
403
+ "$.retries: array found, object expected" );
363
404
364
405
private void addValidationError (String message , String type ) {
365
406
if (skipMessages .contains (message )) {
0 commit comments