|
| 1 | +package ruleopxpropertystringexist |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + |
| 7 | + oas3 "github.com/getkin/kin-openapi/openapi3" |
| 8 | + "github.com/grokify/simplego/net/urlutil" |
| 9 | + "github.com/grokify/spectrum/openapi3" |
| 10 | + "github.com/grokify/spectrum/openapi3lint/lintutil" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + RuleName = "x-operation-x-api-group-exist" |
| 15 | +) |
| 16 | + |
| 17 | +type RuleOperationXPropertyStringExist struct { |
| 18 | + name string |
| 19 | + xPropertyName string |
| 20 | +} |
| 21 | + |
| 22 | +func NewRule(ruleName, xPropertyName string) (RuleOperationXPropertyStringExist, error) { |
| 23 | + ruleName = strings.ToLower(strings.TrimSpace(ruleName)) |
| 24 | + xPropertyName = strings.TrimSpace(xPropertyName) |
| 25 | + |
| 26 | + if len(ruleName) == 0 { |
| 27 | + return RuleOperationXPropertyStringExist{}, |
| 28 | + errors.New("rule name not provided") |
| 29 | + } |
| 30 | + if len(xPropertyName) == 0 { |
| 31 | + return RuleOperationXPropertyStringExist{}, |
| 32 | + errors.New("x-property name not provided") |
| 33 | + } |
| 34 | + |
| 35 | + rule := RuleOperationXPropertyStringExist{ |
| 36 | + name: ruleName, |
| 37 | + xPropertyName: xPropertyName} |
| 38 | + return rule, nil |
| 39 | +} |
| 40 | + |
| 41 | +func (rule RuleOperationXPropertyStringExist) Name() string { |
| 42 | + return rule.name |
| 43 | +} |
| 44 | + |
| 45 | +func (rule RuleOperationXPropertyStringExist) Scope() string { |
| 46 | + return lintutil.ScopeOperation |
| 47 | +} |
| 48 | + |
| 49 | +func (rule RuleOperationXPropertyStringExist) ProcessOperation(spec *oas3.Swagger, op *oas3.Operation, opPointer, path, method string) []lintutil.PolicyViolation { |
| 50 | + if spec == nil || op == nil || len(op.OperationID) == 0 { |
| 51 | + return nil |
| 52 | + } |
| 53 | + prop := strings.TrimSpace( |
| 54 | + openapi3.GetOperationExtensionPropStringOrEmpty(*op, rule.xPropertyName)) |
| 55 | + if len(prop) > 0 { |
| 56 | + return nil |
| 57 | + } |
| 58 | + vio := lintutil.PolicyViolation{ |
| 59 | + RuleName: rule.Name(), |
| 60 | + Location: urlutil.JoinAbsolute(opPointer, "operationId"), |
| 61 | + Value: op.OperationID} |
| 62 | + return []lintutil.PolicyViolation{vio} |
| 63 | +} |
| 64 | + |
| 65 | +func (rule RuleOperationXPropertyStringExist) ProcessSpec(spec *oas3.Swagger, pointerBase string) []lintutil.PolicyViolation { |
| 66 | + return []lintutil.PolicyViolation{} |
| 67 | +} |
0 commit comments