-
-
Notifications
You must be signed in to change notification settings - Fork 39
/
duplicated_enum_entry.go
65 lines (51 loc) · 1.79 KB
/
duplicated_enum_entry.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
// Copyright 2022 Dave Shanley / Quobix
// SPDX-License-Identifier: MIT
package openapi
import (
"fmt"
"github.com/daveshanley/vacuum/model"
vacuumUtils "github.com/daveshanley/vacuum/utils"
"github.com/pb33f/doctor/model/high/base"
"github.com/pb33f/libopenapi/utils"
"gopkg.in/yaml.v3"
)
// DuplicatedEnum will check enum values match the types provided
type DuplicatedEnum struct {
}
// GetSchema returns a model.RuleFunctionSchema defining the schema of the DuplicatedEnum rule.
func (de DuplicatedEnum) GetSchema() model.RuleFunctionSchema {
return model.RuleFunctionSchema{
Name: "duplicatedEnum",
}
}
// GetCategory returns the category of the DuplicatedEnum rule.
func (de DuplicatedEnum) GetCategory() string {
return model.FunctionCategoryOpenAPI
}
// RunRule will execute the DuplicatedEnum rule, based on supplied context and a supplied []*yaml.Node slice.
func (de DuplicatedEnum) RunRule(_ []*yaml.Node, context model.RuleFunctionContext) []model.RuleFunctionResult {
if context.DrDocument == nil {
return nil
}
var results []model.RuleFunctionResult
schemas := context.DrDocument.Schemas
for _, schema := range schemas {
if schema.Value.Enum != nil {
node := schema.Value.GoLow().Enum.KeyNode
duplicates := utils.CheckEnumForDuplicates(schema.Value.Enum)
// iterate through duplicate results and add results.
for _, res := range duplicates {
result := model.RuleFunctionResult{
Message: fmt.Sprintf("enum contains a duplicate: `%s`", res.Value),
StartNode: node,
EndNode: vacuumUtils.BuildEndNode(node),
Path: fmt.Sprintf("%s.%s", schema.GenerateJSONPath(), "enum"),
Rule: context.Rule,
}
schema.AddRuleFunctionResult(base.ConvertRuleResult(&result))
results = append(results, result)
}
}
}
return results
}