-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcomponents.go
99 lines (91 loc) · 2.5 KB
/
components.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
package openapi
// codebeat:disable[TOO_MANY_IVARS]
// Components Object
type Components struct {
Schemas map[string]*Schema
Responses map[string]*Response
Parameters map[string]*Parameter
Examples map[string]*Example
RequestBodies map[string]*RequestBody `yaml:"requestBodies"`
Headers map[string]*Header
SecuritySchemes map[string]*SecurityScheme `yaml:"securitySchemes"`
Links map[string]*Link
Callbacks map[string]*Callback
}
// Validate the values of Components object.
func (components Components) Validate() error {
if err := validateComponentKeys(components); err != nil {
return err
}
validaters := reduceComponentObjects(components)
return validateAll(validaters)
}
func validateComponentKeys(components Components) error {
keys := reduceComponentKeys(components)
for _, k := range keys {
if !mapKeyRegexp.MatchString(k) {
return ErrMapKeyFormat
}
}
return nil
}
func reduceComponentKeys(components Components) []string {
keys := []string{}
for k := range components.Schemas {
keys = append(keys, k)
}
for k := range components.Responses {
keys = append(keys, k)
}
for k := range components.Parameters {
keys = append(keys, k)
}
for k := range components.Examples {
keys = append(keys, k)
}
for k := range components.RequestBodies {
keys = append(keys, k)
}
for k := range components.Headers {
keys = append(keys, k)
}
for k := range components.SecuritySchemes {
keys = append(keys, k)
}
for k := range components.Links {
keys = append(keys, k)
}
for k := range components.Callbacks {
keys = append(keys, k)
}
return keys
}
func reduceComponentObjects(components Components) []validater {
validaters := []validater{}
for _, schema := range components.Schemas {
validaters = append(validaters, schema)
}
for _, response := range components.Responses {
validaters = append(validaters, response)
}
for _, parameter := range components.Parameters {
validaters = append(validaters, parameter)
}
// example has no validation
for _, reqBody := range components.RequestBodies {
validaters = append(validaters, reqBody)
}
for _, header := range components.Headers {
validaters = append(validaters, header)
}
for _, secScheme := range components.SecuritySchemes {
validaters = append(validaters, secScheme)
}
for _, link := range components.Links {
validaters = append(validaters, link)
}
for _, callback := range components.Callbacks {
validaters = append(validaters, callback)
}
return validaters
}