-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: openapi3lint: add
RuleOperationXPropertyStringExist
- Loading branch information
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
openapi3lint/ruleopxpropertystringexist/rule_op_xproperty_string_exist.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package ruleopxpropertystringexist | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
oas3 "github.com/getkin/kin-openapi/openapi3" | ||
"github.com/grokify/simplego/net/urlutil" | ||
"github.com/grokify/spectrum/openapi3" | ||
"github.com/grokify/spectrum/openapi3lint/lintutil" | ||
) | ||
|
||
const ( | ||
RuleName = "x-operation-x-api-group-exist" | ||
) | ||
|
||
type RuleOperationXPropertyStringExist struct { | ||
name string | ||
xPropertyName string | ||
} | ||
|
||
func NewRule(ruleName, xPropertyName string) (RuleOperationXPropertyStringExist, error) { | ||
ruleName = strings.ToLower(strings.TrimSpace(ruleName)) | ||
xPropertyName = strings.TrimSpace(xPropertyName) | ||
|
||
if len(ruleName) == 0 { | ||
return RuleOperationXPropertyStringExist{}, | ||
errors.New("rule name not provided") | ||
} | ||
if len(xPropertyName) == 0 { | ||
return RuleOperationXPropertyStringExist{}, | ||
errors.New("x-property name not provided") | ||
} | ||
|
||
rule := RuleOperationXPropertyStringExist{ | ||
name: ruleName, | ||
xPropertyName: xPropertyName} | ||
return rule, nil | ||
} | ||
|
||
func (rule RuleOperationXPropertyStringExist) Name() string { | ||
return rule.name | ||
} | ||
|
||
func (rule RuleOperationXPropertyStringExist) Scope() string { | ||
return lintutil.ScopeOperation | ||
} | ||
|
||
func (rule RuleOperationXPropertyStringExist) ProcessOperation(spec *oas3.Swagger, op *oas3.Operation, opPointer, path, method string) []lintutil.PolicyViolation { | ||
if spec == nil || op == nil || len(op.OperationID) == 0 { | ||
return nil | ||
} | ||
prop := strings.TrimSpace( | ||
openapi3.GetOperationExtensionPropStringOrEmpty(*op, rule.xPropertyName)) | ||
if len(prop) > 0 { | ||
return nil | ||
} | ||
vio := lintutil.PolicyViolation{ | ||
RuleName: rule.Name(), | ||
Location: urlutil.JoinAbsolute(opPointer, "operationId"), | ||
Value: op.OperationID} | ||
return []lintutil.PolicyViolation{vio} | ||
} | ||
|
||
func (rule RuleOperationXPropertyStringExist) ProcessSpec(spec *oas3.Swagger, pointerBase string) []lintutil.PolicyViolation { | ||
return []lintutil.PolicyViolation{} | ||
} |