-
Notifications
You must be signed in to change notification settings - Fork 28
/
rule_tag_group_exist.go
63 lines (53 loc) · 1.54 KB
/
rule_tag_group_exist.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
package ruletaggroupexist
import (
"strconv"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/mogo/net/urlutil"
"github.com/grokify/spectrum/ext/taggroups"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/openapi3lint/lintutil"
)
const (
RuleName = "x-tag-has-group"
)
type RuleTagHasGroup struct {
name string
}
func NewRule() RuleTagHasGroup {
return RuleTagHasGroup{
name: RuleName}
}
func (rule RuleTagHasGroup) Name() string {
return RuleName
}
func (rule RuleTagHasGroup) Scope() string {
return lintutil.ScopeOperation
}
func (rule RuleTagHasGroup) ProcessOperation(spec *openapi3.Spec, op *oas3.Operation, opPointer, path, method string) []lintutil.PolicyViolation {
if spec == nil || op == nil || len(op.Tags) == 0 {
return nil
}
// sm := openapi3.SpecMore{Spec: spec}
// tagGroups, err := sm.TagGroups()
tagGroups, err := taggroups.SpecTagGroups(spec)
if err != nil {
vio := lintutil.PolicyViolation{
RuleName: rule.Name(),
Location: "#/x-tag-groups",
Value: err.Error()}
return []lintutil.PolicyViolation{vio}
}
vios := []lintutil.PolicyViolation{}
for i, tagName := range op.Tags {
if !tagGroups.Exists(tagName) {
vios = append(vios, lintutil.PolicyViolation{
RuleName: rule.Name(),
Location: urlutil.JoinAbsolute(opPointer, openapi3.PropertyTags, strconv.Itoa(i)),
Value: tagName})
}
}
return vios
}
func (rule RuleTagHasGroup) ProcessSpec(spec *openapi3.Spec, pointerBase string) []lintutil.PolicyViolation {
return []lintutil.PolicyViolation{}
}