@@ -126,8 +126,12 @@ func (e *Env) IsFreezed(t time.Time) (bool, error) {
126
126
return false , nil
127
127
}
128
128
129
- func (e * Env ) EvaluateDynamicPayload (values map [string ]interface {}) (map [string ]interface {}, error ) {
130
- return e .DynamicPayload .Evaluate (values )
129
+ func (e * Env ) IsDynamicPayloadEnabled () bool {
130
+ return (e .DynamicPayload != nil && e .DynamicPayload .Enabled )
131
+ }
132
+
133
+ func (e * Env ) ValidateDynamicPayload (values map [string ]interface {}) error {
134
+ return e .DynamicPayload .Validate (values )
131
135
}
132
136
133
137
// DynamicPayload can be set to dynamically fill in the payload.
@@ -136,9 +140,9 @@ type DynamicPayload struct {
136
140
Inputs map [string ]Input `json:"inputs" yaml:"inputs"`
137
141
}
138
142
139
- // Evaluate validates the payload. After that,
140
- // an object containing only the value of the defined field is returned.
141
- func ( dp * DynamicPayload ) Evaluate ( values map [ string ] interface {}) ( output map [ string ] interface {}, err error ) {
143
+ // Validate validates the payload.
144
+ func ( dp * DynamicPayload ) Validate ( values map [ string ] interface {}) ( err error ) {
145
+
142
146
for key , input := range dp .Inputs {
143
147
// If it is a required field, check if the value exists.
144
148
value , ok := values [key ]
@@ -147,60 +151,60 @@ func (dp *DynamicPayload) Evaluate(values map[string]interface{}) (output map[st
147
151
continue
148
152
}
149
153
150
- return nil , e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%s' field is required." , key ), nil )
154
+ return e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%s' field is required." , key ), nil )
151
155
}
152
156
153
- eval , err := dp .validate (input , value )
157
+ err := dp .validate (input , value )
154
158
if err != nil {
155
- return nil , err
159
+ return err
156
160
}
157
161
158
- output [key ] = eval
159
162
}
160
163
161
- return output , nil
164
+ return nil
162
165
}
163
166
164
- func (dp * DynamicPayload ) validate (input Input , value interface {}) ( interface {}, error ) {
167
+ func (dp * DynamicPayload ) validate (input Input , value interface {}) error {
165
168
switch input .Type {
166
169
case InputTypeSelect :
167
170
// Checks if the selected value matches the option,
168
171
// and returns the value if it is.
169
172
sv , ok := value .(string )
170
173
if ! ok {
171
- return nil , e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not string type." , value ), nil )
174
+ return e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not string type." , value ), nil )
172
175
}
173
176
174
177
for _ , option := range * input .Options {
175
178
if sv == option {
176
- return sv , nil
179
+ return nil
177
180
}
178
181
}
179
182
180
- return nil , e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , "The '%s' is not matched with the options." , nil )
183
+ return e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , "The '%s' is not matched with the options." , nil )
181
184
case InputTypeNumber :
182
- nv , ok := value .(float64 )
183
- if ! ok {
184
- return nil , e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not string type." , value ), nil )
185
+ if _ , ok := value .(float64 ); ok {
186
+ return nil
185
187
}
186
188
187
- return nv , nil
189
+ if _ , ok := value .(int ); ! ok {
190
+ return e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not number type." , value ), nil )
191
+ }
192
+
193
+ return nil
188
194
case InputTypeString :
189
- sv , ok := value .(string )
190
- if ! ok {
191
- return nil , e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not string type." , value ), nil )
195
+ if _ , ok := value .(string ); ! ok {
196
+ return e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not string type." , value ), nil )
192
197
}
193
198
194
- return sv , nil
199
+ return nil
195
200
case InputTypeBoolean :
196
- bv , ok := value .(bool )
197
- if ! ok {
198
- return nil , e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not string type." , value ), nil )
201
+ if _ , ok := value .(bool ); ! ok {
202
+ return e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , fmt .Sprintf ("The '%v' is not string type." , value ), nil )
199
203
}
200
204
201
- return bv , nil
205
+ return nil
202
206
default :
203
- return nil , e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , "The type must be 'select', 'number', 'string', or 'boolean'." , nil )
207
+ return e .NewErrorWithMessage (e .ErrorCodeDeploymentInvalid , "The type must be 'select', 'number', 'string', or 'boolean'." , nil )
204
208
}
205
209
}
206
210
0 commit comments