forked from getkin/kin-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponents.go
105 lines (89 loc) · 2.6 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
100
101
102
103
104
105
package openapi3
import (
"context"
"fmt"
"regexp"
"github.com/getkin/kin-openapi/jsoninfo"
)
// Components is specified by OpenAPI/Swagger standard version 3.0.
type Components struct {
ExtensionProps
Schemas map[string]*SchemaRef `json:"schemas,omitempty"`
Parameters map[string]*ParameterRef `json:"parameters,omitempty"`
Headers map[string]*HeaderRef `json:"headers,omitempty"`
RequestBodies map[string]*RequestBodyRef `json:"requestBodies,omitempty"`
Responses map[string]*ResponseRef `json:"responses,omitempty"`
SecuritySchemes map[string]*SecuritySchemeRef `json:"securitySchemes,omitempty"`
Examples map[string]*ExampleRef `json:"examples,omitempty"`
Tags Tags `json:"tags,omitempty"`
Links map[string]*LinkRef `json:"links,omitempty"`
Callbacks map[string]*CallbackRef `json:"callbacks,omitempty"`
}
func NewComponents() Components {
return Components{}
}
func (components *Components) MarshalJSON() ([]byte, error) {
return jsoninfo.MarshalStrictStruct(components)
}
func (components *Components) UnmarshalJSON(data []byte) error {
return jsoninfo.UnmarshalStrictStruct(data, components)
}
func (components *Components) Validate(c context.Context) (err error) {
for k, v := range components.Schemas {
if err = ValidateIdentifier(k); err != nil {
return
}
if err = v.Validate(c); err != nil {
return
}
}
for k, v := range components.Parameters {
if err = ValidateIdentifier(k); err != nil {
return
}
if err = v.Validate(c); err != nil {
return
}
}
for k, v := range components.RequestBodies {
if err = ValidateIdentifier(k); err != nil {
return
}
if err = v.Validate(c); err != nil {
return
}
}
for k, v := range components.Responses {
if err = ValidateIdentifier(k); err != nil {
return
}
if err = v.Validate(c); err != nil {
return
}
}
for k, v := range components.Headers {
if err = ValidateIdentifier(k); err != nil {
return
}
if err = v.Validate(c); err != nil {
return
}
}
for k, v := range components.SecuritySchemes {
if err = ValidateIdentifier(k); err != nil {
return
}
if err = v.Validate(c); err != nil {
return
}
}
return
}
const identifierPattern = `^[a-zA-Z0-9.\-_]+$`
var identifierRegExp = regexp.MustCompile(identifierPattern)
func ValidateIdentifier(value string) error {
if identifierRegExp.MatchString(value) {
return nil
}
return fmt.Errorf("Identifier '%s' is not supported by OpenAPI version 3 standard (regexp: '%s')", value, identifierPattern)
}