-
Notifications
You must be signed in to change notification settings - Fork 28
/
rule_schema_references_exist.go
61 lines (52 loc) · 1.87 KB
/
rule_schema_references_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
package ruleschemareferences
import (
"fmt"
"strings"
oas3 "github.com/getkin/kin-openapi/openapi3"
"github.com/grokify/spectrum/openapi3"
"github.com/grokify/spectrum/openapi3lint/lintutil"
)
type RuleSchemaReferences struct {
name string
}
func NewRule(ruleName string) (RuleSchemaReferences, error) {
ruleNameCanonical := strings.ToLower(strings.TrimSpace(ruleName))
rule := RuleSchemaReferences{
name: ruleNameCanonical}
if ruleNameCanonical != lintutil.RulenameSchemaHasReference &&
ruleNameCanonical != lintutil.RulenameSchemaReferenceHasSchema {
return rule, fmt.Errorf("rule [%s] not supported", ruleName)
}
return rule, nil
}
func (rule RuleSchemaReferences) Name() string {
return rule.name
}
func (rule RuleSchemaReferences) Scope() string {
return lintutil.ScopeSpecification
}
func (rule RuleSchemaReferences) ProcessOperation(spec *openapi3.Spec, op *oas3.Operation, opPointer, path, method string) []lintutil.PolicyViolation {
return []lintutil.PolicyViolation{}
}
func (rule RuleSchemaReferences) ProcessSpec(spec *openapi3.Spec, pointerBase string) []lintutil.PolicyViolation {
violations := []lintutil.PolicyViolation{}
sm := openapi3.SpecMore{Spec: spec}
schemaNoRef, _, refNoSchema, err := sm.SchemaNamesStatus()
if err != nil {
return violations
}
if rule.name == lintutil.RulenameSchemaHasReference {
for _, schemaName := range schemaNoRef {
violations = append(violations, lintutil.PolicyViolation{
RuleName: lintutil.RulenameSchemaHasReference,
Location: openapi3.SchemaPointerExpand(pointerBase, schemaName)})
}
} else if rule.name == lintutil.RulenameSchemaReferenceHasSchema {
for _, schemaName := range refNoSchema {
violations = append(violations, lintutil.PolicyViolation{
RuleName: lintutil.RulenameSchemaReferenceHasSchema,
Location: openapi3.SchemaPointerExpand(pointerBase, schemaName)})
}
}
return violations
}