-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.go
408 lines (359 loc) · 14.9 KB
/
step.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package arazzo
import (
"context"
"errors"
"fmt"
"regexp"
"github.com/speakeasy-api/openapi/arazzo/core"
"github.com/speakeasy-api/openapi/arazzo/criterion"
"github.com/speakeasy-api/openapi/arazzo/expression"
"github.com/speakeasy-api/openapi/extensions"
"github.com/speakeasy-api/openapi/validation"
)
// Steps represents a list of Step objects that describe the operations to be performed in the workflow.
type Steps []*Step
// Find will return the first Step object with the provided id.
func (s Steps) Find(id string) *Step {
for _, step := range s {
if step.StepID == id {
return step
}
}
return nil
}
// Step represents a step in a workflow that describes the operation to be performed.
type Step struct {
// StepID is a unique identifier for the step within a workflow.
StepID string
// Description is a description of the step.
Description *string
// OperationID is an operationId or expression to an operation in a SourceDescription that the step relates to. Mutually exclusive with OperationPath & WorkflowID.
OperationID *expression.Expression
// OperationPath is an expression to an operation in a SourceDescription that the step relates to. Mutually exclusive with OperationID & WorkflowID.
OperationPath *expression.Expression
// WorkflowID is a workflowId or expression to a workflow in a SourceDescription that the step relates to. Mutually exclusive with OperationID & OperationPath.
WorkflowID *expression.Expression
// Parameters is a list of Parameters that will be passed to the referenced operation or workflow. These will override any matching parameters defined at the workflow level.
Parameters []*ReusableParameter
// RequestBody is the request body to be passed to the referenced operation.
RequestBody *RequestBody
// SuccessCriteria is a list of criteria that must be met for the step to be considered successful.
SuccessCriteria []*criterion.Criterion
// OnSuccess is a list of SuccessActions that will be executed if the step is successful.
OnSuccess []*ReusableSuccessAction
// OnFailure is a list of FailureActions that will be executed if the step is unsuccessful.
OnFailure []*ReusableFailureAction
// Outputs is a list of outputs that will be returned by the step.
Outputs Outputs
// Extensions provides a list of extensions to the Step object.
Extensions *extensions.Extensions
// Valid indicates whether this model passed validation.
Valid bool
core core.Step
}
var _ model[core.Step] = (*Step)(nil)
// GetCore will return the low level representation of the step object.
// Useful for accessing line and column numbers for various nodes in the backing yaml/json document.
func (s *Step) GetCore() *core.Step {
return &s.core
}
var stepIDRegex = regexp.MustCompile(`^[A-Za-z0-9_\-]+$`)
// Validate will validate the step object against the Arazzo specification.
// Requires an Arazzo object to be passed via validation options with validation.WithContextObject().
// Requires a Workflow object to be passed via validation options with validation.WithContextObject().
func (s *Step) Validate(ctx context.Context, opts ...validation.Option) []error {
o := validation.NewOptions(opts...)
a := validation.GetContextObject[Arazzo](o)
w := validation.GetContextObject[Workflow](o)
if a == nil {
return []error{
errors.New("An Arazzo object must be passed via validation options to validate a Step"),
}
}
if w == nil {
return []error{
errors.New("A Workflow object must be passed via validation options to validate a Step"),
}
}
opts = append(opts, validation.WithContextObject(s))
errs := []error{}
if s.core.StepID.Present && s.StepID == "" {
errs = append(errs, &validation.Error{
Message: "stepId is required",
Line: s.core.StepID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.StepID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
} else if s.StepID != "" {
if !stepIDRegex.MatchString(s.StepID) {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("stepId must be a valid name [%s]: %s", stepIDRegex.String(), s.StepID),
Line: s.core.StepID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.StepID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
numStepsWithID := 0
for _, step := range w.Steps {
if step.StepID == s.StepID {
numStepsWithID++
}
}
if numStepsWithID > 1 {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("stepId must be unique within the workflow, found %d steps with the same stepId", numStepsWithID),
Line: s.core.StepID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.StepID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
}
targetExpressions := []*expression.Expression{
s.OperationID,
s.OperationPath,
s.WorkflowID,
}
numSet := 0
for _, expression := range targetExpressions {
if expression != nil {
numSet++
}
}
switch numSet {
case 0:
errs = append(errs, &validation.Error{
Message: "at least one of operationId, operationPath or workflowId must be set",
Line: s.core.RootNode.Line,
Column: s.core.RootNode.Column,
})
case 1:
default:
errs = append(errs, &validation.Error{
Message: "only one of operationId, operationPath or workflowId can be set",
Line: s.core.RootNode.Line,
Column: s.core.RootNode.Column,
})
}
if s.OperationID != nil {
numOpenAPISourceDescriptions := 0
for _, sourceDescription := range a.SourceDescriptions {
if sourceDescription.Type == SourceDescriptionTypeOpenAPI {
numOpenAPISourceDescriptions++
}
}
if numOpenAPISourceDescriptions > 1 && !s.OperationID.IsExpression() {
errs = append(errs, &validation.Error{
Message: "operationId must be a valid expression if there are multiple OpenAPI source descriptions",
Line: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if s.OperationID.IsExpression() {
if err := s.OperationID.Validate(false); err != nil {
errs = append(errs, &validation.Error{
Message: err.Error(),
Line: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
typ, sourceDescriptionName, _, _ := s.OperationID.GetParts()
if typ != expression.ExpressionTypeSourceDescriptions {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("operationId must be a sourceDescriptions expression, got %s", typ),
Line: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if a.SourceDescriptions.Find(string(sourceDescriptionName)) == nil {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("sourceDescription %s not found", sourceDescriptionName),
Line: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
}
}
if s.OperationPath != nil {
if err := s.OperationPath.Validate(true); err != nil {
errs = append(errs, &validation.Error{
Message: err.Error(),
Line: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
typ, sourceDescriptionName, expressionParts, jp := s.OperationPath.GetParts()
if typ != expression.ExpressionTypeSourceDescriptions {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("operationPath must be a sourceDescriptions expression, got %s", typ),
Line: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if a.SourceDescriptions.Find(string(sourceDescriptionName)) == nil {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("sourceDescription %s not found", sourceDescriptionName),
Line: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if len(expressionParts) != 1 || expressionParts[0] != "url" {
errs = append(errs, &validation.Error{
Message: "operationPath must reference the url of a sourceDescription",
Line: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if jp == "" {
errs = append(errs, &validation.Error{
Message: "operationPath must contain a json pointer to the operation path within the sourceDescription",
Line: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.OperationPath.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
}
if s.WorkflowID != nil {
if s.WorkflowID.IsExpression() {
if err := s.WorkflowID.Validate(false); err != nil {
errs = append(errs, &validation.Error{
Message: err.Error(),
Line: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
typ, sourceDescriptionName, _, _ := s.WorkflowID.GetParts()
if typ != expression.ExpressionTypeSourceDescriptions {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("workflowId must be a sourceDescriptions expression, got %s", typ),
Line: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
if a.SourceDescriptions.Find(string(sourceDescriptionName)) == nil {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("sourceDescription %s not found", sourceDescriptionName),
Line: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
} else {
if a.Workflows.Find(string(*s.WorkflowID)) == nil {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("workflow %s not found", *s.WorkflowID),
Line: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.WorkflowID.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
}
}
parameterRefs := make(map[string]bool)
parameters := make(map[string]bool)
for i, parameter := range s.Parameters {
errs = append(errs, parameter.Validate(ctx, opts...)...)
if parameter.Reference != nil {
_, ok := parameterRefs[string(*parameter.Reference)]
if ok {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("duplicate parameter found with reference %s", *parameter.Reference),
Line: s.core.Parameters.GetSliceValueNodeOrRoot(i, s.core.RootNode).Line,
Column: s.core.Parameters.GetSliceValueNodeOrRoot(i, s.core.RootNode).Column,
})
}
parameterRefs[string(*parameter.Reference)] = true
} else if parameter.Object != nil {
id := fmt.Sprintf("%s.%v", parameter.Object.Name, parameter.Object.In)
_, ok := parameters[id]
if ok {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("duplicate parameter found with name %s and in %v", parameter.Object.Name, parameter.Object.In),
Line: s.core.Parameters.GetSliceValueNodeOrRoot(i, s.core.RootNode).Line,
Column: s.core.Parameters.GetSliceValueNodeOrRoot(i, s.core.RootNode).Column,
})
}
parameters[id] = true
}
}
if s.RequestBody != nil {
if s.WorkflowID != nil {
errs = append(errs, &validation.Error{
Message: "requestBody should not be set when workflowId is set",
Line: s.core.RequestBody.GetValueNodeOrRoot(s.core.RootNode).Line,
Column: s.core.RequestBody.GetValueNodeOrRoot(s.core.RootNode).Column,
})
}
errs = append(errs, s.RequestBody.Validate(ctx, opts...)...)
}
for _, successCriterion := range s.SuccessCriteria {
errs = append(errs, successCriterion.Validate(opts...)...)
}
successActionRefs := make(map[string]bool)
successActions := make(map[string]bool)
for i, onSuccess := range s.OnSuccess {
errs = append(errs, onSuccess.Validate(ctx, opts...)...)
if onSuccess.Reference != nil {
_, ok := successActionRefs[string(*onSuccess.Reference)]
if ok {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("duplicate successAction found with reference %s", *onSuccess.Reference),
Line: s.core.OnSuccess.GetSliceValueNodeOrRoot(i, s.core.RootNode).Line,
Column: s.core.OnSuccess.GetSliceValueNodeOrRoot(i, s.core.RootNode).Column,
})
}
successActionRefs[string(*onSuccess.Reference)] = true
} else if onSuccess.Object != nil {
id := fmt.Sprintf("%s.%v", onSuccess.Object.Name, onSuccess.Object.Type)
_, ok := successActions[id]
if ok {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("duplicate successAction found with name %s and type %v", onSuccess.Object.Name, onSuccess.Object.Type),
Line: s.core.OnSuccess.GetSliceValueNodeOrRoot(i, s.core.RootNode).Line,
Column: s.core.OnSuccess.GetSliceValueNodeOrRoot(i, s.core.RootNode).Column,
})
}
successActions[id] = true
}
}
failureActionRefs := make(map[string]bool)
failureActions := make(map[string]bool)
for i, onFailure := range s.OnFailure {
errs = append(errs, onFailure.Validate(ctx, opts...)...)
if onFailure.Reference != nil {
_, ok := failureActionRefs[string(*onFailure.Reference)]
if ok {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("duplicate failureAction found with reference %s", *onFailure.Reference),
Line: s.core.OnFailure.GetSliceValueNodeOrRoot(i, s.core.RootNode).Line,
Column: s.core.OnFailure.GetSliceValueNodeOrRoot(i, s.core.RootNode).Column,
})
}
failureActionRefs[string(*onFailure.Reference)] = true
} else if onFailure.Object != nil {
id := fmt.Sprintf("%s.%v", onFailure.Object.Name, onFailure.Object.Type)
_, ok := failureActions[id]
if ok {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("duplicate failureAction found with name %s and type %v", onFailure.Object.Name, onFailure.Object.Type),
Line: s.core.OnFailure.GetSliceValueNodeOrRoot(i, s.core.RootNode).Line,
Column: s.core.OnFailure.GetSliceValueNodeOrRoot(i, s.core.RootNode).Column,
})
}
failureActions[id] = true
}
}
for name, output := range s.Outputs.All() {
if !outputNameRegex.MatchString(name) {
errs = append(errs, &validation.Error{
Message: fmt.Sprintf("output name must be a valid name [%s]: %s", outputNameRegex.String(), name),
Line: s.core.Outputs.GetMapKeyNodeOrRoot(name, s.core.RootNode).Line,
Column: s.core.Outputs.GetMapKeyNodeOrRoot(name, s.core.RootNode).Column,
})
}
if err := output.Validate(true); err != nil {
errs = append(errs, &validation.Error{
Message: err.Error(),
Line: s.core.Outputs.GetMapValueNodeOrRoot(name, s.core.RootNode).Line,
Column: s.core.Outputs.GetMapValueNodeOrRoot(name, s.core.RootNode).Column,
})
}
}
if len(errs) == 0 {
s.Valid = true
}
return errs
}