Skip to content

Commit e8de77e

Browse files
committed
refactor: add more consts and use them
1 parent cadff75 commit e8de77e

15 files changed

Lines changed: 90 additions & 82 deletions

errors/validation_error.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package errors
66
import (
77
"fmt"
88

9+
"github.com/pb33f/libopenapi-validator/helpers"
910
"github.com/santhosh-tekuri/jsonschema/v6"
1011
)
1112

@@ -128,10 +129,10 @@ func (v *ValidationError) Error() string {
128129

129130
// IsPathMissingError returns true if the error has a ValidationType of "path" and a ValidationSubType of "missing"
130131
func (v *ValidationError) IsPathMissingError() bool {
131-
return v.ValidationType == "path" && v.ValidationSubType == "missing"
132+
return v.ValidationType == helpers.PathValidation && v.ValidationSubType == helpers.ValidationMissing
132133
}
133134

134135
// IsOperationMissingError returns true if the error has a ValidationType of "request" and a ValidationSubType of "missingOperation"
135136
func (v *ValidationError) IsOperationMissingError() bool {
136-
return v.ValidationType == "path" && v.ValidationSubType == "missingOperation"
137+
return v.ValidationType == helpers.PathValidation && v.ValidationSubType == helpers.ValidationMissingOperation
137138
}

errors/validation_error_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"testing"
99

10+
"github.com/pb33f/libopenapi-validator/helpers"
1011
"github.com/stretchr/testify/require"
1112
)
1213

@@ -82,8 +83,8 @@ func TestValidationError_Error_WithSchemaValidationErrors_AndSpecLineColumn(t *t
8283
func TestValidationError_IsPathMissingError(t *testing.T) {
8384
// Test the IsPathMissingError method
8485
v := &ValidationError{
85-
ValidationType: "path",
86-
ValidationSubType: "missing",
86+
ValidationType: helpers.PathValidation,
87+
ValidationSubType: helpers.ValidationMissing,
8788
}
8889

8990
require.True(t, v.IsPathMissingError())
@@ -93,16 +94,16 @@ func TestValidationError_IsPathMissingError(t *testing.T) {
9394
require.False(t, v.IsPathMissingError())
9495

9596
// Test with different ValidationType
96-
v.ValidationType = "request"
97-
v.ValidationSubType = "missing"
97+
v.ValidationType = helpers.RequestValidation
98+
v.ValidationSubType = helpers.ValidationMissing
9899
require.False(t, v.IsPathMissingError())
99100
}
100101

101102
func TestValidationError_IsOperationMissingError(t *testing.T) {
102103
// Test the IsOperationMissingError method
103104
v := &ValidationError{
104-
ValidationType: "path",
105-
ValidationSubType: "missingOperation",
105+
ValidationType: helpers.PathValidation,
106+
ValidationSubType: helpers.ValidationMissingOperation,
106107
}
107108

108109
require.True(t, v.IsOperationMissingError())
@@ -112,7 +113,7 @@ func TestValidationError_IsOperationMissingError(t *testing.T) {
112113
require.False(t, v.IsOperationMissingError())
113114

114115
// Test with different ValidationType
115-
v.ValidationType = "request"
116-
v.ValidationSubType = "missingOperation"
116+
v.ValidationType = helpers.RequestValidation
117+
v.ValidationSubType = helpers.ValidationMissingOperation
117118
require.False(t, v.IsOperationMissingError())
118119
}

helpers/constants.go

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,39 +14,45 @@ const (
1414
Schema = "schema"
1515
ResponseBodyValidation = "response"
1616
RequestBodyContentType = "contentType"
17-
RequestMissingOperation = "missingOperation"
18-
ResponseBodyResponseCode = "statusCode"
19-
SpaceDelimited = "spaceDelimited"
20-
PipeDelimited = "pipeDelimited"
21-
DefaultDelimited = "default"
22-
MatrixStyle = "matrix"
23-
LabelStyle = "label"
24-
Pipe = "|"
25-
Comma = ","
26-
Space = " "
27-
SemiColon = ";"
28-
Asterisk = "*"
29-
Period = "."
30-
Equals = "="
31-
Integer = "integer"
32-
Number = "number"
33-
Slash = "/"
34-
Object = "object"
35-
String = "string"
36-
Array = "array"
37-
Boolean = "boolean"
38-
DeepObject = "deepObject"
39-
Header = "header"
40-
Cookie = "cookie"
41-
Path = "path"
42-
Form = "form"
43-
Query = "query"
44-
JSONContentType = "application/json"
45-
JSONType = "json"
46-
ContentTypeHeader = "Content-Type"
47-
AuthorizationHeader = "Authorization"
48-
Charset = "charset"
49-
Boundary = "boundary"
50-
Preferred = "preferred"
51-
FailSegment = "**&&FAIL&&**"
17+
// Deprecated: use ValidationMissingOperation
18+
RequestMissingOperation = "missingOperation"
19+
PathValidation = "path"
20+
ValidationMissing = "missing"
21+
ValidationMissingOperation = "missingOperation"
22+
ResponseBodyResponseCode = "statusCode"
23+
SecurityValidation = "security"
24+
DocumentValidation = "document"
25+
SpaceDelimited = "spaceDelimited"
26+
PipeDelimited = "pipeDelimited"
27+
DefaultDelimited = "default"
28+
MatrixStyle = "matrix"
29+
LabelStyle = "label"
30+
Pipe = "|"
31+
Comma = ","
32+
Space = " "
33+
SemiColon = ";"
34+
Asterisk = "*"
35+
Period = "."
36+
Equals = "="
37+
Integer = "integer"
38+
Number = "number"
39+
Slash = "/"
40+
Object = "object"
41+
String = "string"
42+
Array = "array"
43+
Boolean = "boolean"
44+
DeepObject = "deepObject"
45+
Header = "header"
46+
Cookie = "cookie"
47+
Path = "path"
48+
Form = "form"
49+
Query = "query"
50+
JSONContentType = "application/json"
51+
JSONType = "json"
52+
ContentTypeHeader = "Content-Type"
53+
AuthorizationHeader = "Authorization"
54+
Charset = "charset"
55+
Boundary = "boundary"
56+
Preferred = "preferred"
57+
FailSegment = "**&&FAIL&&**"
5258
)

parameters/cookie_parameters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (v *paramValidator) ValidateCookieParamsWithPathItem(request *http.Request,
3131
if pathItem == nil {
3232
return false, []*errors.ValidationError{{
3333
ValidationType: helpers.ParameterValidationPath,
34-
ValidationSubType: "missing",
34+
ValidationSubType: helpers.ValidationMissing,
3535
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
3636
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
3737
"however that path, or the %s method for that path does not exist in the specification",

parameters/header_parameters.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (v *paramValidator) ValidateHeaderParamsWithPathItem(request *http.Request,
3232
if pathItem == nil {
3333
return false, []*errors.ValidationError{{
3434
ValidationType: helpers.ParameterValidationPath,
35-
ValidationSubType: "missing",
35+
ValidationSubType: helpers.ValidationMissing,
3636
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
3737
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
3838
"however that path, or the %s method for that path does not exist in the specification",

parameters/path_parameters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func (v *paramValidator) ValidatePathParams(request *http.Request) (bool, []*err
3131
func (v *paramValidator) ValidatePathParamsWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) {
3232
if pathItem == nil {
3333
return false, []*errors.ValidationError{{
34-
ValidationType: helpers.ParameterValidationPath,
35-
ValidationSubType: "missing",
34+
ValidationType: helpers.PathValidation,
35+
ValidationSubType: helpers.ValidationMissing,
3636
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
3737
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
3838
"however that path, or the %s method for that path does not exist in the specification",

parameters/query_parameters.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ func (v *paramValidator) ValidateQueryParams(request *http.Request) (bool, []*er
3737
func (v *paramValidator) ValidateQueryParamsWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) {
3838
if pathItem == nil {
3939
return false, []*errors.ValidationError{{
40-
ValidationType: helpers.ParameterValidationPath,
41-
ValidationSubType: "missing",
40+
ValidationType: helpers.PathValidation,
41+
ValidationSubType: helpers.ValidationMissing,
4242
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
4343
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
4444
"however that path, or the %s method for that path does not exist in the specification",

parameters/validate_security.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func (v *paramValidator) ValidateSecurity(request *http.Request) (bool, []*error
2828
func (v *paramValidator) ValidateSecurityWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) {
2929
if pathItem == nil {
3030
return false, []*errors.ValidationError{{
31-
ValidationType: helpers.ParameterValidationPath,
32-
ValidationSubType: "missing",
31+
ValidationType: helpers.PathValidation,
32+
ValidationSubType: helpers.ValidationMissing,
3333
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
3434
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
3535
"however that path, or the %s method for that path does not exist in the specification",
@@ -71,7 +71,7 @@ func (v *paramValidator) ValidateSecurityWithPathItem(request *http.Request, pat
7171
Message: fmt.Sprintf("Security scheme '%s' is missing", secName),
7272
Reason: fmt.Sprintf("The security scheme '%s' is defined as being required, "+
7373
"however it's missing from the components", secName),
74-
ValidationType: "security",
74+
ValidationType: helpers.SecurityValidation,
7575
SpecLine: sec.GoLow().Requirements.ValueNode.Line,
7676
SpecCol: sec.GoLow().Requirements.ValueNode.Column,
7777
HowToFix: "Add the missing security scheme to the components",
@@ -131,7 +131,7 @@ func (v *paramValidator) validateHTTPSecurityScheme(
131131
{
132132
Message: fmt.Sprintf("Authorization header for '%s' scheme", secScheme.Scheme),
133133
Reason: "Authorization header was not found",
134-
ValidationType: "security",
134+
ValidationType: helpers.SecurityValidation,
135135
ValidationSubType: secScheme.Scheme,
136136
SpecLine: sec.GoLow().Requirements.ValueNode.Line,
137137
SpecCol: sec.GoLow().Requirements.ValueNode.Column,
@@ -159,7 +159,7 @@ func (v *paramValidator) validateAPIKeySecurityScheme(
159159
{
160160
Message: fmt.Sprintf("API Key %s not found in header", secScheme.Name),
161161
Reason: "API Key not found in http header for security scheme 'apiKey' with type 'header'",
162-
ValidationType: "security",
162+
ValidationType: helpers.SecurityValidation,
163163
ValidationSubType: "apiKey",
164164
SpecLine: sec.GoLow().Requirements.ValueNode.Line,
165165
SpecCol: sec.GoLow().Requirements.ValueNode.Column,
@@ -183,7 +183,7 @@ func (v *paramValidator) validateAPIKeySecurityScheme(
183183
{
184184
Message: fmt.Sprintf("API Key %s not found in query", secScheme.Name),
185185
Reason: "API Key not found in URL query for security scheme 'apiKey' with type 'query'",
186-
ValidationType: "security",
186+
ValidationType: helpers.SecurityValidation,
187187
ValidationSubType: "apiKey",
188188
SpecLine: sec.GoLow().Requirements.ValueNode.Line,
189189
SpecCol: sec.GoLow().Requirements.ValueNode.Column,
@@ -207,7 +207,7 @@ func (v *paramValidator) validateAPIKeySecurityScheme(
207207
{
208208
Message: fmt.Sprintf("API Key %s not found in cookies", secScheme.Name),
209209
Reason: "API Key not found in http request cookies for security scheme 'apiKey' with type 'cookie'",
210-
ValidationType: "security",
210+
ValidationType: helpers.SecurityValidation,
211211
ValidationSubType: "apiKey",
212212
SpecLine: sec.GoLow().Requirements.ValueNode.Line,
213213
SpecCol: sec.GoLow().Requirements.ValueNode.Column,

paths/paths.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func FindPath(request *http.Request, document *v3.Document, regexCache config.Re
7070
if len(candidates) == 0 {
7171
validationErrors := []*errors.ValidationError{
7272
{
73-
ValidationType: helpers.ParameterValidationPath,
74-
ValidationSubType: "missing",
73+
ValidationType: helpers.PathValidation,
74+
ValidationSubType: helpers.ValidationMissing,
7575
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
7676
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
7777
"however that path, or the %s method for that path does not exist in the specification",
@@ -93,8 +93,8 @@ func FindPath(request *http.Request, document *v3.Document, regexCache config.Re
9393

9494
// path matches exist but none have the required method
9595
validationErrors := []*errors.ValidationError{{
96-
ValidationType: helpers.ParameterValidationPath,
97-
ValidationSubType: "missingOperation",
96+
ValidationType: helpers.PathValidation,
97+
ValidationSubType: helpers.ValidationMissingOperation,
9898
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
9999
Reason: fmt.Sprintf("The %s method for that path does not exist in the specification",
100100
request.Method),

requests/validate_body.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func (v *requestBodyValidator) ValidateRequestBody(request *http.Request) (bool,
2727
func (v *requestBodyValidator) ValidateRequestBodyWithPathItem(request *http.Request, pathItem *v3.PathItem, pathValue string) (bool, []*errors.ValidationError) {
2828
if pathItem == nil {
2929
return false, []*errors.ValidationError{{
30-
ValidationType: helpers.ParameterValidationPath,
31-
ValidationSubType: "missing",
30+
ValidationType: helpers.PathValidation,
31+
ValidationSubType: helpers.ValidationMissing,
3232
Message: fmt.Sprintf("%s Path '%s' not found", request.Method, request.URL.Path),
3333
Reason: fmt.Sprintf("The %s request contains a path of '%s' "+
3434
"however that path, or the %s method for that path does not exist in the specification",

0 commit comments

Comments
 (0)