-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdocument.go
117 lines (106 loc) · 2.81 KB
/
document.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
package openapi
import (
"sort"
"strconv"
"strings"
)
// codebeat:disable[TOO_MANY_IVARS]
// Document represents a OpenAPI Specification document.
type Document struct {
Version string `yaml:"openapi"`
Info *Info
Servers []*Server
Paths Paths
Components *Components
Security []*SecurityRequirement
Tags []*Tag
ExternalDocs *ExternalDocumentation `yaml:"externalDocs"`
}
// Validate the values of spec.
func (doc Document) Validate() error {
if err := doc.validateRequiredFields(); err != nil {
return err
}
if err := doc.validateOASVersion(); err != nil {
return err
}
return doc.validateFields()
}
func (doc Document) validateOASVersion() error {
splited := strings.FieldsFunc(doc.Version, func(r rune) bool { return r == '.' })
if len(splited) != 3 {
return ErrFormatInvalid{Target: "openapi version", Format: "X.Y.Z"}
}
major, err := strconv.Atoi(splited[0])
if err != nil {
return ErrFormatInvalid{Target: "major part of openapi version"}
}
minor, err := strconv.Atoi(splited[1])
if err != nil {
return ErrFormatInvalid{Target: "minor part of openapi version"}
}
_, err = strconv.Atoi(splited[2])
if err != nil {
return ErrFormatInvalid{Target: "patch part of openapi version"}
}
if major == 3 && 0 <= minor {
return nil
}
return ErrUnsupportedVersion
}
func (doc Document) validateRequiredFields() error {
if doc.Version == "" {
return ErrRequired{Target: "openapi"}
}
if doc.Info == nil {
return ErrRequired{Target: "info"}
}
if doc.Paths == nil {
return ErrRequired{Target: "paths"}
}
return nil
}
func (doc Document) validateFields() error {
var validaters []validater
validaters = append(validaters, doc.Info)
for _, s := range doc.Servers {
validaters = append(validaters, s)
}
validaters = append(validaters, doc.Paths)
if doc.Components != nil {
validaters = append(validaters, doc.Components)
}
for _, securityRequirement := range doc.Security {
validaters = append(validaters, securityRequirement)
}
for _, t := range doc.Tags {
validaters = append(validaters, t)
}
if doc.ExternalDocs != nil {
validaters = append(validaters, doc.ExternalDocs)
}
return validateAll(validaters)
}
type WalkFunc func(doc *Document, method, path string, pathItem *PathItem, op *Operation) error
func (doc *Document) Walk(walkFn WalkFunc) error {
var paths []string
for path := range doc.Paths {
paths = append(paths, path)
}
sort.Strings(paths)
for _, path := range paths {
pathItem := doc.Paths[path]
var methods []string
for method := range pathItem.Operations() {
methods = append(methods, method)
}
sort.Strings(methods)
for _, method := range methods {
operation := pathItem.GetOperationByMethod(method)
if err := walkFn(doc, method, path, pathItem, operation); err != nil {
return err
}
}
}
return nil
}