-
Notifications
You must be signed in to change notification settings - Fork 28
/
rule_tag_style_first_uppercase.go
69 lines (61 loc) · 1.79 KB
/
rule_tag_style_first_uppercase.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
package ruletagstylefirstuppercase
import (
"strconv"
"github.com/grokify/mogo/encoding/jsonpointer"
"github.com/grokify/mogo/text/stringcase"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/openapi3lint/lintutil"
)
type RuleTagStyleFirstUpperCase struct {
name string
}
func NewRule() RuleTagStyleFirstUpperCase {
return RuleTagStyleFirstUpperCase{
name: lintutil.RulenameTagStyleFirstUpperCase}
}
func (rule RuleTagStyleFirstUpperCase) Name() string {
return rule.name
}
func (rule RuleTagStyleFirstUpperCase) Scope() string {
return lintutil.ScopeOperation
}
func (rule RuleTagStyleFirstUpperCase) ProcessOperation(spec *openapi3.Spec, op *openapi3.Operation, opPointer, path, method string) []lintutil.PolicyViolation {
return nil
}
func (rule RuleTagStyleFirstUpperCase) ProcessSpec(spec *openapi3.Spec, pointerBase string) []lintutil.PolicyViolation {
vios := []lintutil.PolicyViolation{}
openapi3.VisitOperations(spec, func(path, method string, op *openapi3.Operation) {
if op == nil {
return
}
opLoc := jsonpointer.PointerSubEscapeAll(
"%s#/paths/%s/%s/tags/",
pointerBase,
path,
method)
for i, tag := range op.Tags {
if !stringcase.IsFirstAlphaUpper(tag) {
vios = append(vios, lintutil.PolicyViolation{
RuleName: rule.Name(),
Location: opLoc + strconv.Itoa(i),
Value: tag})
}
}
})
for i, tag := range spec.Tags {
jsLoc := jsonpointer.PointerSubEscapeAll(
"%s#/tags/%d/name", pointerBase, i)
if tag == nil {
vios = append(vios, lintutil.PolicyViolation{
RuleName: rule.Name(),
Location: jsLoc,
Value: ""})
} else if !stringcase.IsFirstAlphaUpper(tag.Name) {
vios = append(vios, lintutil.PolicyViolation{
RuleName: rule.Name(),
Location: jsLoc,
Value: tag.Name})
}
}
return vios
}