This repository has been archived by the owner on Jul 13, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
199 lines (151 loc) · 4.2 KB
/
main.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bytes"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"regexp"
"strings"
)
type (
Resource string
Method string
Code string
Violations map[Resource][]string
Schema struct {
Type string `json:"type"`
Ref string `json:"$ref"`
}
Parameter struct {
In string `json:"in"`
Schema Schema `json:"schema"`
Name string `json:"name"`
}
Response struct {
Schema Schema `json:"schema"`
}
Responses map[Code]Response
Path struct {
Tags []string `json:"tags"`
OperationID string `json:"operationId"`
Parameters []Parameter `json:"parameters"`
Responses Responses `json:"responses"`
}
Paths map[Method]Path
Swagger struct {
Paths map[Resource]Paths `json:"paths"`
}
)
func main() {
var input string
flag.StringVar(&input, "input", "", "Swagger 2.0 JSON file.")
flag.Parse()
//-
b, err := ioutil.ReadFile(input)
if err != nil {
log.Fatalf("Reading input: %s", err)
}
//-
var res Swagger
if err := json.NewDecoder(bytes.NewReader(b)).Decode(&res); err != nil {
log.Fatalf("Decoding %s", err)
}
errors := res.Validate()
var count int
for path, errs := range errors {
fmt.Printf("\n%s\n", path)
for _, err := range errs {
fmt.Printf("\t%s\n", err)
count++
}
}
if len(errors) > 0 {
fmt.Printf("\nTotal violations: %d\n", count)
os.Exit(1)
}
fmt.Println("File follows the expected Swagger 2.0 rules")
}
func (s Swagger) Validate() Violations {
var res Violations = make(map[Resource][]string)
for resource, paths := range s.Paths {
var violations []string
for verb, path := range paths {
if vs := s.validateOperationID(verb, path); len(vs) > 0 {
violations = append(violations, vs...)
}
if vs := s.validateParameters(path); len(vs) > 0 {
violations = append(violations, vs...)
}
if vs := s.validateResponses(path); len(vs) > 0 {
violations = append(violations, vs...)
}
if len(violations) > 0 {
res[resource] = violations
}
}
}
return res
}
func (s Swagger) validateOperationID(method Method, path Path) []string {
res := make([]string, 0, 3)
if path.OperationID == "" {
res = append(res, "Missing operation id.")
} else if strings.ToLower(path.OperationID)[0:len(method)] != string(method) {
res = append(res, newViolation(path.OperationID, "Resource must begin with '%s'.", method))
}
if len(path.Tags) == 0 {
res = append(res, "Resource must define at least one tag.")
}
return res
}
func (s Swagger) validateParameters(path Path) []string {
res := make([]string, 0, 2)
for _, parameter := range path.Parameters {
if parameter.In == "body" && parameter.Schema.Ref != "" {
if !matchRegEx(parameter.Schema.Ref, `^(#\/definitions\/%sRequest)`, path.OperationID) {
res = append(res,
newViolation(path.OperationID,
"Body request model must be prefixed with method+Request: '%s'.", parameter.Schema.Ref))
}
}
if parameter.In == "query" && strings.ToLower(parameter.Name) != parameter.Name {
res = append(res,
newViolation(path.OperationID, "Query arguments must be lowercase: '%s'", parameter.Name))
}
}
return res
}
func (s Swagger) validateResponses(path Path) []string {
res := make([]string, 0, 2)
for code, response := range path.Responses {
if strings.HasPrefix(string(code), "2") {
if response.Schema.Type == "array" {
res = append(res,
newViolation(path.OperationID, "Instead of using Array as a response, prefer defining a new model."))
}
if response.Schema.Ref != "" {
if !matchRegEx(response.Schema.Ref, `^(#\/definitions\/%sResponse)`, path.OperationID) {
res = append(res,
newViolation(path.OperationID,
"Code %s, response model must be prefixed with method+Response: '%s'.", code, response.Schema.Ref))
}
}
}
}
return res
}
func matchRegEx(value, format, operationID string) bool {
prefix := strings.Title(operationID)
methodRegEx := regexp.MustCompile(fmt.Sprintf(format, prefix))
return methodRegEx.MatchString(value)
}
func newViolation(operationID string, format string, a ...interface{}) string {
var prefix string
if operationID != "" {
prefix = fmt.Sprintf("'%s': ", operationID)
}
return fmt.Sprintf("%s%s", prefix, fmt.Sprintf(format, a...))
}