Skip to content

Commit a0d862a

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

7 files changed

Lines changed: 25 additions & 22 deletions

File tree

errors/validation_error_test.go

Lines changed: 3 additions & 2 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,7 +83,7 @@ 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+
ValidationType: helpers.ParameterValidationPath,
8687
ValidationSubType: "missing",
8788
}
8889

@@ -101,7 +102,7 @@ func TestValidationError_IsPathMissingError(t *testing.T) {
101102
func TestValidationError_IsOperationMissingError(t *testing.T) {
102103
// Test the IsOperationMissingError method
103104
v := &ValidationError{
104-
ValidationType: "path",
105+
ValidationType: helpers.ParameterValidationPath,
105106
ValidationSubType: "missingOperation",
106107
}
107108

helpers/constants.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const (
1616
RequestBodyContentType = "contentType"
1717
RequestMissingOperation = "missingOperation"
1818
ResponseBodyResponseCode = "statusCode"
19+
SecurityValidation = "security"
20+
DocumentValidation = "document"
1921
SpaceDelimited = "spaceDelimited"
2022
PipeDelimited = "pipeDelimited"
2123
DefaultDelimited = "default"

parameters/validate_security.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

schema_validation/validate_document.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func ValidateOpenAPIDocument(doc libopenapi.Document, opts ...config.Option) (bo
4646
ReferenceSchema: loadedSchema,
4747
}
4848
validationErrors = append(validationErrors, &liberrors.ValidationError{
49-
ValidationType: "schema",
49+
ValidationType: helpers.Schema,
5050
ValidationSubType: "compilation",
5151
Message: "OpenAPI document schema compilation failed",
5252
Reason: fmt.Sprintf("The OpenAPI schema failed to compile: %s", err.Error()),

schema_validation/validate_document_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func validateOpenAPIDocumentWithMalformedSchema(loadedSchema string, decodedDocu
6868
ReferenceSchema: loadedSchema,
6969
}
7070
validationErrors = append(validationErrors, &liberrors.ValidationError{
71-
ValidationType: "schema",
71+
ValidationType: helpers.Schema,
7272
ValidationSubType: "compilation",
7373
Message: "OpenAPI document schema compilation failed",
7474
Reason: fmt.Sprintf("The OpenAPI schema failed to compile: %s", err.Error()),

validator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func (v *validator) GetResponseBodyValidator() responses.ResponseBodyValidator {
124124
func (v *validator) ValidateDocument() (bool, []*errors.ValidationError) {
125125
if v.document == nil {
126126
return false, []*errors.ValidationError{{
127-
ValidationType: "document",
127+
ValidationType: helpers.DocumentValidation,
128128
ValidationSubType: "missing",
129129
Message: "Document is not set",
130130
Reason: "The document cannot be validated as it is not set",

validator_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,25 +2400,25 @@ paths:
24002400
func TestSortValidationErrors(t *testing.T) {
24012401
// Create errors in random order
24022402
errs := []*errors.ValidationError{
2403-
{ValidationType: "security", Message: "API Key missing"},
2404-
{ValidationType: "parameter", Message: "Path param invalid"},
2405-
{ValidationType: "request", Message: "Body invalid"},
2406-
{ValidationType: "parameter", Message: "Header missing"},
2407-
{ValidationType: "security", Message: "Auth header missing"},
2403+
{ValidationType: helpers.SecurityValidation, Message: "API Key missing"},
2404+
{ValidationType: helpers.ParameterValidation, Message: "Path param invalid"},
2405+
{ValidationType: helpers.RequestValidation, Message: "Body invalid"},
2406+
{ValidationType: helpers.ParameterValidation, Message: "Header missing"},
2407+
{ValidationType: helpers.SecurityValidation, Message: "Auth header missing"},
24082408
}
24092409

24102410
sortValidationErrors(errs)
24112411

24122412
// Verify sorted by validation type first, then by message
2413-
assert.Equal(t, "parameter", errs[0].ValidationType)
2413+
assert.Equal(t, helpers.ParameterValidation, errs[0].ValidationType)
24142414
assert.Equal(t, "Header missing", errs[0].Message)
2415-
assert.Equal(t, "parameter", errs[1].ValidationType)
2415+
assert.Equal(t, helpers.ParameterValidation, errs[1].ValidationType)
24162416
assert.Equal(t, "Path param invalid", errs[1].Message)
2417-
assert.Equal(t, "request", errs[2].ValidationType)
2417+
assert.Equal(t, helpers.RequestValidation, errs[2].ValidationType)
24182418
assert.Equal(t, "Body invalid", errs[2].Message)
2419-
assert.Equal(t, "security", errs[3].ValidationType)
2419+
assert.Equal(t, helpers.SecurityValidation, errs[3].ValidationType)
24202420
assert.Equal(t, "API Key missing", errs[3].Message)
2421-
assert.Equal(t, "security", errs[4].ValidationType)
2421+
assert.Equal(t, helpers.SecurityValidation, errs[4].ValidationType)
24222422
assert.Equal(t, "Auth header missing", errs[4].Message)
24232423
}
24242424

@@ -2432,9 +2432,9 @@ func TestSortValidationErrors_Empty(t *testing.T) {
24322432
// TestSortValidationErrors_SingleElement tests sorting single element slice
24332433
func TestSortValidationErrors_SingleElement(t *testing.T) {
24342434
errs := []*errors.ValidationError{
2435-
{ValidationType: "parameter", Message: "Invalid value"},
2435+
{ValidationType: helpers.ParameterValidation, Message: "Invalid value"},
24362436
}
24372437
sortValidationErrors(errs)
24382438
assert.Len(t, errs, 1)
2439-
assert.Equal(t, "parameter", errs[0].ValidationType)
2439+
assert.Equal(t, helpers.ParameterValidation, errs[0].ValidationType)
24402440
}

0 commit comments

Comments
 (0)