-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathparameter.go
75 lines (64 loc) · 1.82 KB
/
parameter.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
package openapi
// codebeat:disable[TOO_MANY_IVARS]
// Parameter Object
type Parameter struct {
Name string
In InType
Description string
Required bool
Deprecated string
AllowEmptyValue bool `yaml:"allowEmptyValue"`
Style string
Explode bool
AllowReserved bool `yaml:"allowReserved"`
Schema *Schema
Example interface{}
Examples map[string]*Example
Content map[string]*MediaType
Ref string `yaml:"$ref"`
}
// Validate the values of Parameter object.
// This function DOES NOT check whether the name field correspond to the associated path or not.
func (parameter Parameter) Validate() error {
if err := parameter.validateRequiredObjects(); err != nil {
return err
}
switch parameter.In {
case InQuery, InHeader, InPath, InCookie:
default:
return ErrMustOneOf{Object: "parameter.in", ValidValues: ParameterInList}
}
if parameter.In == InPath && !parameter.Required {
return ErrRequiredMustTrue
}
if parameter.In != InQuery && parameter.AllowEmptyValue {
return ErrAllowEmptyValueNotValid
}
if len(parameter.Content) > 1 {
return ErrTooManyParameterContent
}
return validateAll(parameter.reduceValidaters())
}
func (parameter Parameter) validateRequiredObjects() error {
if parameter.Name == "" {
return ErrRequired{Target: "parameter.name"}
}
if parameter.In == "" {
return ErrRequired{Target: "parameter.in"}
}
return nil
}
func (parameter Parameter) reduceValidaters() []validater {
validaters := []validater{}
if parameter.Schema != nil {
validaters = append(validaters, parameter.Schema)
}
if v, ok := parameter.Example.(validater); ok {
validaters = append(validaters, v)
}
// example has no validation
for _, mediaType := range parameter.Content {
validaters = append(validaters, mediaType)
}
return validaters
}