| 1 |
// Copyright 2023-2024 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// https://pb33f.io |
| 3 |
|
| 4 |
package errors |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi-validator/helpers" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func TestSchemaValidationFailure_Error(t *testing.T) { |
| 15 |
// Test the Error method of SchemaValidationFailure |
| 16 |
s := &SchemaValidationFailure{ |
| 17 |
Reason: "Invalid type", |
| 18 |
FieldPath: "$.path.to.property", |
| 19 |
} |
| 20 |
|
| 21 |
expectedError := "Reason: Invalid type, FieldPath: $.path.to.property" |
| 22 |
require.Equal(t, expectedError, s.Error()) |
| 23 |
} |
| 24 |
|
| 25 |
func TestValidationError_Error_NoSchemaValidationErrors(t *testing.T) { |
| 26 |
// Test the Error method of ValidationError with no SchemaValidationErrors and no line/column info |
| 27 |
v := &ValidationError{ |
| 28 |
Message: "Missing required field", |
| 29 |
Reason: "The field 'id' is required but missing", |
| 30 |
} |
| 31 |
|
| 32 |
expectedError := "Error: Missing required field, Reason: The field 'id' is required but missing" |
| 33 |
require.Equal(t, expectedError, v.Error()) |
| 34 |
} |
| 35 |
|
| 36 |
func TestValidationError_Error_WithSpecLineAndColumn(t *testing.T) { |
| 37 |
// Test the Error method of ValidationError with spec line and column |
| 38 |
v := &ValidationError{ |
| 39 |
Message: "Invalid data type", |
| 40 |
Reason: "Expected 'string', got 'integer'", |
| 41 |
SpecLine: 10, |
| 42 |
SpecCol: 15, |
| 43 |
} |
| 44 |
|
| 45 |
expectedError := "Error: Invalid data type, Reason: Expected 'string', got 'integer', Line: 10, Column: 15" |
| 46 |
require.Equal(t, expectedError, v.Error()) |
| 47 |
} |
| 48 |
|
| 49 |
func TestValidationError_Error_WithSchemaValidationErrors(t *testing.T) { |
| 50 |
// Test the Error method of ValidationError with SchemaValidationErrors |
| 51 |
schemaError := &SchemaValidationFailure{ |
| 52 |
Reason: "Invalid enum value", |
| 53 |
FieldPath: "$.path.to.enum", |
| 54 |
} |
| 55 |
v := &ValidationError{ |
| 56 |
Message: "Enum validation failed", |
| 57 |
Reason: "Invalid enum value", |
| 58 |
SchemaValidationErrors: []*SchemaValidationFailure{schemaError}, |
| 59 |
} |
| 60 |
|
| 61 |
expectedError := fmt.Sprintf("Error: Enum validation failed, Reason: Invalid enum value, Validation Errors: %s", []*SchemaValidationFailure{schemaError}) |
| 62 |
require.Equal(t, expectedError, v.Error()) |
| 63 |
} |
| 64 |
|
| 65 |
func TestValidationError_Error_WithSchemaValidationErrors_AndSpecLineColumn(t *testing.T) { |
| 66 |
// Test the Error method of ValidationError with SchemaValidationErrors and SpecLine and SpecCol |
| 67 |
schemaError := &SchemaValidationFailure{ |
| 68 |
Reason: "Invalid enum value", |
| 69 |
FieldPath: "$.path.to.enum", |
| 70 |
} |
| 71 |
v := &ValidationError{ |
| 72 |
Message: "Enum validation failed", |
| 73 |
Reason: "Invalid enum value", |
| 74 |
SchemaValidationErrors: []*SchemaValidationFailure{schemaError}, |
| 75 |
SpecLine: 12, |
| 76 |
SpecCol: 5, |
| 77 |
} |
| 78 |
|
| 79 |
expectedError := fmt.Sprintf("Error: Enum validation failed, Reason: Invalid enum value, Validation Errors: %s, Line: 12, Column: 5", []*SchemaValidationFailure{schemaError}) |
| 80 |
require.Equal(t, expectedError, v.Error()) |
| 81 |
} |
| 82 |
|
| 83 |
func TestValidationError_IsPathMissingError(t *testing.T) { |
| 84 |
// Test the IsPathMissingError method |
| 85 |
v := &ValidationError{ |
| 86 |
ValidationType: helpers.PathValidation, |
| 87 |
ValidationSubType: helpers.ValidationMissing, |
| 88 |
} |
| 89 |
|
| 90 |
require.True(t, v.IsPathMissingError()) |
| 91 |
|
| 92 |
// Test with different ValidationSubType |
| 93 |
v.ValidationSubType = "wrongType" |
| 94 |
require.False(t, v.IsPathMissingError()) |
| 95 |
|
| 96 |
// Test with different ValidationType |
| 97 |
v.ValidationType = helpers.RequestValidation |
| 98 |
v.ValidationSubType = helpers.ValidationMissing |
| 99 |
require.False(t, v.IsPathMissingError()) |
| 100 |
} |
| 101 |
|
| 102 |
func TestValidationError_IsOperationMissingError(t *testing.T) { |
| 103 |
// Test the IsOperationMissingError method |
| 104 |
v := &ValidationError{ |
| 105 |
ValidationType: helpers.PathValidation, |
| 106 |
ValidationSubType: helpers.ValidationMissingOperation, |
| 107 |
} |
| 108 |
|
| 109 |
require.True(t, v.IsOperationMissingError()) |
| 110 |
|
| 111 |
// Test with different ValidationSubType |
| 112 |
v.ValidationSubType = "wrongOperation" |
| 113 |
require.False(t, v.IsOperationMissingError()) |
| 114 |
|
| 115 |
// Test with different ValidationType |
| 116 |
v.ValidationType = helpers.RequestValidation |
| 117 |
v.ValidationSubType = helpers.ValidationMissingOperation |
| 118 |
require.False(t, v.IsOperationMissingError()) |
| 119 |
} |