-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathparameter.go
More file actions
102 lines (85 loc) · 3.66 KB
/
Copy pathparameter.go
File metadata and controls
102 lines (85 loc) · 3.66 KB
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
package arazzo
import (
"context"
"errors"
"fmt"
"strings"
"github.com/speakeasy-api/openapi/arazzo/core"
"github.com/speakeasy-api/openapi/expression"
"github.com/speakeasy-api/openapi/extensions"
"github.com/speakeasy-api/openapi/internal/interfaces"
"github.com/speakeasy-api/openapi/marshaller"
"github.com/speakeasy-api/openapi/validation"
)
// In represents the location of a parameter.
type In string
const (
// InPath indicates that the parameter is in the path of the request.
InPath In = "path"
// InQuery indicates that the parameter is in the query of the request.
InQuery In = "query"
// InHeader indicates that the parameter is in the header of the request.
InHeader In = "header"
// InCookie indicates that the parameter is in the cookie of the request.
InCookie In = "cookie"
)
// Parameter represents parameters that will be passed to a workflow or operation referenced by a step.
type Parameter struct {
marshaller.Model[core.Parameter]
// Name is the case-sensitive name of the parameter.
Name string
// In is the location of the parameter within an operation.
In *In
// Value represents either the static value of the parameter or an expression that will be evaluated to produce the value.
Value expression.ValueOrExpression
// Extensions provides a list of extensions to the Parameter object.
Extensions *extensions.Extensions
}
var _ interfaces.Model[core.Parameter] = (*Parameter)(nil)
// Validate will validate the parameter object against the Arazzo specification.
// If an Workflow or Step object is provided via validation options with validation.WithContextObject() then
// it will be validated in the context of that object.
func (p *Parameter) Validate(ctx context.Context, opts ...validation.Option) []error {
core := p.GetCore()
errs := []error{}
o := validation.NewOptions(opts...)
w := validation.GetContextObject[Workflow](o)
s := validation.GetContextObject[Step](o)
if core.Name.Present && p.Name == "" {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("parameter fieldname is required"), core, core.Name))
}
in := In("")
if p.In != nil {
in = *p.In
}
switch in {
case InPath:
case InQuery:
case InHeader:
case InCookie:
default:
if p.In == nil || in == "" {
if w == nil && s != nil && s.WorkflowID == nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("parameter.in is required within a step when workflowId is not set"), core, core.In))
}
}
if in != "" {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationAllowedValues, fmt.Errorf("parameter.in must be one of [`%s`] but was `%s`", strings.Join([]string{string(InPath), string(InQuery), string(InHeader), string(InCookie)}, ", "), in), core, core.In))
}
}
if core.Value.Present && p.Value == nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`parameter.value` is required"), core, core.Value))
} else if p.Value != nil {
_, expression, err := expression.GetValueOrExpressionValue(p.Value)
if err != nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationInvalidSyntax, fmt.Errorf("%s", err.Error()), core, core.Value))
}
if expression != nil {
if err := expression.Validate(); err != nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationInvalidSyntax, fmt.Errorf("%s", err.Error()), core, core.Value))
}
}
}
p.Valid = len(errs) == 0 && core.GetValid()
return errs
}