Skip to content

Commit b5f89fa

Browse files
authored
fix(*): validate failed when transition is missing (#75)
Signed-off-by: lsytj0413 <511121939@qq.com> Signed-off-by: lsytj0413 <511121939@qq.com>
1 parent cc7e1a3 commit b5f89fa

File tree

4 files changed

+104
-22
lines changed

4 files changed

+104
-22
lines changed

model/states.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type EventState struct {
135135
// Define the events to be consumed and optional actions to be performed
136136
OnEvents []OnEvents `json:"onEvents" validate:"required,min=1,dive"`
137137
// State specific timeouts
138-
Timeout EventStateTimeout `json:"timeouts,omitempty"`
138+
Timeout *EventStateTimeout `json:"timeouts,omitempty"`
139139
}
140140

141141
// UnmarshalJSON ...
@@ -187,7 +187,7 @@ type OperationState struct {
187187
// Actions to be performed
188188
Actions []Action `json:"actions" validate:"required,min=1,dive"`
189189
// State specific timeouts
190-
Timeouts OperationStateTimeout `json:"timeouts,omitempty"`
190+
Timeouts *OperationStateTimeout `json:"timeouts,omitempty"`
191191
}
192192

193193
// OperationStateTimeout ...
@@ -206,7 +206,7 @@ type ParallelState struct {
206206
// Used when completionType is set to 'atLeast' to specify the minimum number of branches that must complete before the state will transition."
207207
NumCompleted intstr.IntOrString `json:"numCompleted,omitempty"`
208208
// State specific timeouts
209-
Timeouts ParallelStateTimeout `json:"timeouts,omitempty"`
209+
Timeouts *ParallelStateTimeout `json:"timeouts,omitempty"`
210210
}
211211

212212
// ParallelStateTimeout ...
@@ -221,7 +221,7 @@ type InjectState struct {
221221
// JSON object which can be set as states data input and can be manipulated via filters
222222
Data map[string]interface{} `json:"data" validate:"required,min=1"`
223223
// State specific timeouts
224-
Timeouts InjectStateTimeout `json:"timeouts,omitempty"`
224+
Timeouts *InjectStateTimeout `json:"timeouts,omitempty"`
225225
}
226226

227227
// InjectStateTimeout ...
@@ -243,7 +243,7 @@ type ForEachState struct {
243243
// Actions to be executed for each of the elements of inputCollection
244244
Actions []Action `json:"actions,omitempty"`
245245
// State specific timeout
246-
Timeouts ForEachStateTimeout `json:"timeouts,omitempty"`
246+
Timeouts *ForEachStateTimeout `json:"timeouts,omitempty"`
247247
// Mode Specifies how iterations are to be performed (sequentially or in parallel)
248248
Mode ForEachModeType `json:"mode,omitempty"`
249249
}
@@ -280,7 +280,7 @@ type SleepState struct {
280280
// Duration (ISO 8601 duration format) to sleep
281281
Duration string `json:"duration" validate:"required"`
282282
// Timeouts State specific timeouts
283-
Timeouts SleepStateTimeout `json:"timeouts,omitempty"`
283+
Timeouts *SleepStateTimeout `json:"timeouts,omitempty"`
284284
}
285285

286286
// SleepStateTimeout ...
@@ -301,7 +301,7 @@ type EventBasedSwitchState struct {
301301
// Defines conditions evaluated against events
302302
EventConditions []EventCondition `json:"eventConditions" validate:"required,min=1,dive"`
303303
// State specific timeouts
304-
Timeouts EventBasedSwitchStateTimeout `json:"timeouts,omitempty"`
304+
Timeouts *EventBasedSwitchStateTimeout `json:"timeouts,omitempty"`
305305
}
306306

307307
// UnmarshalJSON implementation for json Unmarshal function for the Eventbasedswitch type
@@ -395,8 +395,8 @@ type EndEventCondition struct {
395395
// DataBasedSwitchState Permits transitions to other states based on data conditions
396396
type DataBasedSwitchState struct {
397397
BaseSwitchState
398-
DataConditions []DataCondition `json:"dataConditions" validate:"required,min=1,dive"`
399-
Timeouts DataBasedSwitchStateTimeout `json:"timeouts,omitempty"`
398+
DataConditions []DataCondition `json:"dataConditions" validate:"required,min=1,dive"`
399+
Timeouts *DataBasedSwitchStateTimeout `json:"timeouts,omitempty"`
400400
}
401401

402402
// UnmarshalJSON implementation for json Unmarshal function for the Databasedswitch type

model/workflow.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type BaseWorkflow struct {
9595
// Workflow base definition
9696
type Workflow struct {
9797
BaseWorkflow
98-
States []State `json:"states" validate:"required,min=1"`
98+
States []State `json:"states" validate:"required,min=1,dive"`
9999
Events []Event `json:"events,omitempty"`
100100
Functions []Function `json:"functions,omitempty"`
101101
Retries []Retry `json:"retries,omitempty" validate:"dive"`
@@ -381,8 +381,8 @@ func (s *Start) UnmarshalJSON(data []byte) error {
381381

382382
// DefaultCondition Can be either a transition or end definition
383383
type DefaultCondition struct {
384-
Transition Transition `json:"transition,omitempty"`
385-
End End `json:"end,omitempty"`
384+
Transition *Transition `json:"transition,omitempty"`
385+
End *End `json:"end,omitempty"`
386386
}
387387

388388
// Schedule ...
@@ -504,12 +504,12 @@ type OnEvents struct {
504504
// Action ...
505505
type Action struct {
506506
// Unique action definition name
507-
Name string `json:"name,omitempty"`
508-
FunctionRef FunctionRef `json:"functionRef,omitempty"`
507+
Name string `json:"name,omitempty"`
508+
FunctionRef *FunctionRef `json:"functionRef,omitempty"`
509509
// References a 'trigger' and 'result' reusable event definitions
510-
EventRef EventRef `json:"eventRef,omitempty"`
510+
EventRef *EventRef `json:"eventRef,omitempty"`
511511
// References a sub-workflow to be executed
512-
SubFlowRef WorkflowRef `json:"subFlowRef,omitempty"`
512+
SubFlowRef *WorkflowRef `json:"subFlowRef,omitempty"`
513513
// Sleep Defines time period workflow execution should sleep before / after function execution
514514
Sleep Sleep `json:"sleep,omitempty"`
515515
// RetryRef References a defined workflow retry definition. If not defined the default retry policy is assumed
@@ -529,8 +529,8 @@ type End struct {
529529
// Defines events that should be produced
530530
ProduceEvents []ProduceEvent `json:"produceEvents,omitempty"`
531531
// If set to true, triggers workflow compensation. Default is false
532-
Compensate bool `json:"compensate,omitempty"`
533-
ContinueAs ContinueAs `json:"continueAs,omitempty"`
532+
Compensate bool `json:"compensate,omitempty"`
533+
ContinueAs *ContinueAs `json:"continueAs,omitempty"`
534534
}
535535

536536
// UnmarshalJSON ...

parser/parser_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ func TestBasicValidation(t *testing.T) {
3030
for _, file := range files {
3131
if !file.IsDir() {
3232
workflow, err := FromFile(filepath.Join(rootPath, file.Name()))
33-
if assert.NoError(t, err) {
34-
assert.NotEmpty(t, workflow.Name)
35-
assert.NotEmpty(t, workflow.ID)
36-
assert.NotEmpty(t, workflow.States)
33+
if assert.NoError(t, err, "Test File %s", file.Name()) {
34+
assert.NotEmpty(t, workflow.Name, "Test File %s", file.Name())
35+
assert.NotEmpty(t, workflow.ID, "Test File %s", file.Name())
36+
assert.NotEmpty(t, workflow.States, "Test File %s", file.Name())
3737
}
3838
}
3939
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
{
2+
"id": "applicantrequest",
3+
"version": "1.0",
4+
"name": "Applicant Request Decision Workflow",
5+
"description": "Determine if applicant request is valid",
6+
"start": "CheckApplication",
7+
"specVersion": "0.7",
8+
"auth": [{
9+
"name": "testAuth",
10+
"scheme": "bearer",
11+
"properties": {
12+
"token": "test_token"
13+
}
14+
}],
15+
"functions": [
16+
{
17+
"name": "sendRejectionEmailFunction",
18+
"operation": "http://myapis.org/applicationapi.json#emailRejection"
19+
}
20+
],
21+
"retries": [
22+
{
23+
"name": "TimeoutRetryStrategy",
24+
"delay": "PT1M",
25+
"maxAttempts": "5"
26+
}
27+
],
28+
"states": [
29+
{
30+
"name": "CheckApplication",
31+
"type": "switch",
32+
"dataConditions": [
33+
{
34+
"condition": "${ .applicants | .age >= 18 }"
35+
},
36+
{
37+
"condition": "${ .applicants | .age < 18 }",
38+
"transition": {
39+
"nextState": "RejectApplication"
40+
}
41+
}
42+
],
43+
"default": {
44+
"transition": {
45+
"nextState": "RejectApplication"
46+
}
47+
}
48+
},
49+
{
50+
"name": "StartApplication",
51+
"type": "operation",
52+
"actions": [
53+
{
54+
"subFlowRef": {
55+
"workflowId": "startApplicationWorkflowId"
56+
}
57+
}
58+
],
59+
"end": {
60+
"terminate": true
61+
}
62+
},
63+
{
64+
"name": "RejectApplication",
65+
"type": "operation",
66+
"actionMode": "sequential",
67+
"actions": [
68+
{
69+
"functionRef": {
70+
"refName": "sendRejectionEmailFunction",
71+
"parameters": {
72+
"applicant": "${ .applicant }"
73+
}
74+
}
75+
}
76+
],
77+
"end": {
78+
"terminate": true
79+
}
80+
}
81+
]
82+
}

0 commit comments

Comments
 (0)