@@ -75,68 +75,84 @@ func (c *ValidatorContext) init(workflow *Workflow) {
75
75
}
76
76
77
77
func (c * ValidatorContext ) ExistState (name string ) bool {
78
+ if c .States == nil {
79
+ return true
80
+ }
78
81
_ , ok := c .States [name ]
79
82
return ok
80
83
}
81
84
82
85
func (c * ValidatorContext ) ExistFunction (name string ) bool {
86
+ if c .Functions == nil {
87
+ return true
88
+ }
83
89
_ , ok := c .Functions [name ]
84
90
return ok
85
91
}
86
92
87
93
func (c * ValidatorContext ) ExistEvent (name string ) bool {
94
+ if c .Events == nil {
95
+ return true
96
+ }
88
97
_ , ok := c .Events [name ]
89
98
return ok
90
99
}
91
100
92
101
func (c * ValidatorContext ) ExistRetry (name string ) bool {
102
+ if c .Retries == nil {
103
+ return true
104
+ }
93
105
_ , ok := c .Retries [name ]
94
106
return ok
95
107
}
96
108
97
109
func (c * ValidatorContext ) ExistError (name string ) bool {
110
+ if c .Errors == nil {
111
+ return true
112
+ }
98
113
_ , ok := c .Errors [name ]
99
114
return ok
100
115
}
101
116
102
- func NewValidatorContext (workflow * Workflow ) context.Context {
103
- for i := range workflow .States {
104
- s := & workflow .States [i ]
105
- if s .BaseState .Transition != nil {
106
- s .BaseState .Transition .stateParent = s
107
- }
108
- for _ , onError := range s .BaseState .OnErrors {
109
- if onError .Transition != nil {
110
- onError .Transition .stateParent = s
111
- }
112
- }
113
- if s .Type == StateTypeSwitch {
114
- if s .SwitchState .DefaultCondition .Transition != nil {
115
- s .SwitchState .DefaultCondition .Transition .stateParent = s
117
+ func NewValidatorContext (object any ) context.Context {
118
+ contextValue := ValidatorContext {}
119
+
120
+ if workflow , ok := object .(* Workflow ); ok {
121
+ for i := range workflow .States {
122
+ s := & workflow .States [i ]
123
+ if s .BaseState .Transition != nil {
124
+ s .BaseState .Transition .stateParent = s
116
125
}
117
- for _ , e := range s .SwitchState . EventConditions {
118
- if e .Transition != nil {
119
- e .Transition .stateParent = s
126
+ for _ , onError := range s .BaseState . OnErrors {
127
+ if onError .Transition != nil {
128
+ onError .Transition .stateParent = s
120
129
}
121
130
}
122
- for _ , d := range s .SwitchState .DataConditions {
123
- if d .Transition != nil {
124
- d .Transition .stateParent = s
131
+ if s .Type == StateTypeSwitch {
132
+ if s .SwitchState .DefaultCondition .Transition != nil {
133
+ s .SwitchState .DefaultCondition .Transition .stateParent = s
134
+ }
135
+ for _ , e := range s .SwitchState .EventConditions {
136
+ if e .Transition != nil {
137
+ e .Transition .stateParent = s
138
+ }
139
+ }
140
+ for _ , d := range s .SwitchState .DataConditions {
141
+ if d .Transition != nil {
142
+ d .Transition .stateParent = s
143
+ }
125
144
}
126
145
}
127
146
}
147
+ contextValue .init (workflow )
128
148
}
129
149
130
- contextValue := ValidatorContext {}
131
- contextValue .init (workflow )
132
-
133
150
return context .WithValue (context .Background (), ValidatorContextValue , contextValue )
134
151
}
135
152
136
153
func init () {
137
154
// TODO: create states graph to complex check
138
155
139
- // val.GetValidator().RegisterStructValidationCtx(val.ValidationWrap(nil, workflowStructLevelValidation), Workflow{})
140
156
val .GetValidator ().RegisterStructValidationCtx (ValidationWrap (onErrorStructLevelValidationCtx ), OnError {})
141
157
val .GetValidator ().RegisterStructValidationCtx (ValidationWrap (transitionStructLevelValidationCtx ), Transition {})
142
158
val .GetValidator ().RegisterStructValidationCtx (ValidationWrap (startStructLevelValidationCtx ), Start {})
0 commit comments