Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Working memory and debug artifacts
431 changes: 395 additions & 36 deletions errors/parameter_errors.go

Large diffs are not rendered by default.

72 changes: 36 additions & 36 deletions errors/parameter_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func TestQueryParameterMissing(t *testing.T) {
param := createMockParameterWithSchema()

// Call the function
err := QueryParameterMissing(param)
err := QueryParameterMissing(param, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -134,7 +134,7 @@ func TestHeaderParameterMissing(t *testing.T) {
param := createMockParameterWithSchema()

// Call the function
err := HeaderParameterMissing(param)
err := HeaderParameterMissing(param, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -150,7 +150,7 @@ func TestHeaderParameterCannotBeDecoded(t *testing.T) {
val := "malformed_header_value"

// Call the function
err := HeaderParameterCannotBeDecoded(param, val)
err := HeaderParameterCannotBeDecoded(param, val, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestIncorrectHeaderParamEnum(t *testing.T) {
schema := base.NewSchema(s)

// Call the function with an invalid enum value
err := IncorrectHeaderParamEnum(param, "invalidEnum", schema)
err := IncorrectHeaderParamEnum(param, "invalidEnum", schema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestIncorrectQueryParamArrayBoolean(t *testing.T) {
schema := base.NewSchema(s)

// Call the function with an invalid boolean value in the array
err := IncorrectQueryParamArrayBoolean(param, "notBoolean", schema, schema.Items.A.Schema())
err := IncorrectQueryParamArrayBoolean(param, "notBoolean", schema, schema.Items.A.Schema(), "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -314,7 +314,7 @@ func TestIncorrectCookieParamArrayBoolean(t *testing.T) {
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())

// Call the function with an invalid boolean value in the array
err := IncorrectCookieParamArrayBoolean(param, "notBoolean", s, itemsSchema)
err := IncorrectCookieParamArrayBoolean(param, "notBoolean", s, itemsSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -375,7 +375,7 @@ func TestIncorrectQueryParamArrayInteger(t *testing.T) {
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())

// Call the function with an invalid number value in the array
err := IncorrectQueryParamArrayInteger(param, "notNumber", s, itemsSchema)
err := IncorrectQueryParamArrayInteger(param, "notNumber", s, itemsSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -394,7 +394,7 @@ func TestIncorrectQueryParamArrayNumber(t *testing.T) {
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())

// Call the function with an invalid number value in the array
err := IncorrectQueryParamArrayNumber(param, "notNumber", s, itemsSchema)
err := IncorrectQueryParamArrayNumber(param, "notNumber", s, itemsSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -455,7 +455,7 @@ func TestIncorrectCookieParamArrayNumber(t *testing.T) {
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())

// Call the function with an invalid number value in the cookie array
err := IncorrectCookieParamArrayNumber(param, "notNumber", s, itemsSchema)
err := IncorrectCookieParamArrayNumber(param, "notNumber", s, itemsSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -531,7 +531,7 @@ func TestIncorrectParamEncodingJSON(t *testing.T) {
baseSchema := createMockLowBaseSchema()

// Call the function with an invalid JSON value
err := IncorrectParamEncodingJSON(param, "invalidJSON", base.NewSchema(baseSchema))
err := IncorrectParamEncodingJSON(param, "invalidJSON", base.NewSchema(baseSchema), "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -560,7 +560,7 @@ func TestIncorrectQueryParamBool(t *testing.T) {
})

// Call the function with an invalid boolean value
err := IncorrectQueryParamBool(param, "notBoolean", base.NewSchema(baseSchema))
err := IncorrectQueryParamBool(param, "notBoolean", base.NewSchema(baseSchema), "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -576,7 +576,7 @@ func TestInvalidQueryParamNumber(t *testing.T) {
baseSchema := createMockLowBaseSchema()

// Call the function with an invalid number value
err := InvalidQueryParamNumber(param, "notNumber", base.NewSchema(baseSchema))
err := InvalidQueryParamNumber(param, "notNumber", base.NewSchema(baseSchema), "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -592,7 +592,7 @@ func TestInvalidQueryParamInteger(t *testing.T) {
baseSchema := createMockLowBaseSchema()

// Call the function with an invalid number value
err := InvalidQueryParamInteger(param, "notNumber", base.NewSchema(baseSchema))
err := InvalidQueryParamInteger(param, "notNumber", base.NewSchema(baseSchema), "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -617,7 +617,7 @@ func TestIncorrectQueryParamEnum(t *testing.T) {
param.GoLow().Schema.Value.Schema().Enum.KeyNode = &yaml.Node{}

// Call the function with an invalid enum value
err := IncorrectQueryParamEnum(param, "invalidEnum", highSchema)
err := IncorrectQueryParamEnum(param, "invalidEnum", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -646,7 +646,7 @@ func TestIncorrectQueryParamEnumArray(t *testing.T) {
}

// Call the function with an invalid enum value
err := IncorrectQueryParamEnumArray(param, "invalidEnum", highSchema)
err := IncorrectQueryParamEnumArray(param, "invalidEnum", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -669,7 +669,7 @@ func TestIncorrectReservedValues(t *testing.T) {
param := createMockParameter()
param.Name = "borked::?^&*"

err := IncorrectReservedValues(param, "borked::?^&*", highSchema)
err := IncorrectReservedValues(param, "borked::?^&*", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -692,7 +692,7 @@ func TestInvalidHeaderParamInteger(t *testing.T) {
param := createMockParameter()
param.Name = "bunny"

err := InvalidHeaderParamInteger(param, "bunmy", highSchema)
err := InvalidHeaderParamInteger(param, "bunmy", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -715,7 +715,7 @@ func TestInvalidHeaderParamNumber(t *testing.T) {
param := createMockParameter()
param.Name = "bunny"

err := InvalidHeaderParamNumber(param, "bunmy", highSchema)
err := InvalidHeaderParamNumber(param, "bunmy", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -738,7 +738,7 @@ func TestInvalidCookieParamNumber(t *testing.T) {
param := createMockParameter()
param.Name = "cookies"

err := InvalidCookieParamNumber(param, "milky", highSchema)
err := InvalidCookieParamNumber(param, "milky", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -761,7 +761,7 @@ func TestInvalidCookieParamInteger(t *testing.T) {
param := createMockParameter()
param.Name = "cookies"

err := InvalidCookieParamInteger(param, "milky", highSchema)
err := InvalidCookieParamInteger(param, "milky", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -784,7 +784,7 @@ func TestIncorrectHeaderParamBool(t *testing.T) {
param := createMockParameter()
param.Name = "cookies"

err := IncorrectHeaderParamBool(param, "milky", highSchema)
err := IncorrectHeaderParamBool(param, "milky", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -807,7 +807,7 @@ func TestIncorrectCookieParamBool(t *testing.T) {
param := createMockParameter()
param.Name = "cookies"

err := IncorrectCookieParamBool(param, "milky", highSchema)
err := IncorrectCookieParamBool(param, "milky", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -837,7 +837,7 @@ items:
}
param.GoLow().Schema.Value.Schema().Enum.KeyNode = &yaml.Node{}

err := IncorrectCookieParamEnum(param, "milky", highSchema)
err := IncorrectCookieParamEnum(param, "milky", highSchema, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -863,7 +863,7 @@ func TestIncorrectHeaderParamArrayBoolean(t *testing.T) {
param := createMockParameter()
param.Name = "bubbles"

err := IncorrectHeaderParamArrayBoolean(param, "milky", highSchema, nil)
err := IncorrectHeaderParamArrayBoolean(param, "milky", highSchema, nil, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -889,7 +889,7 @@ func TestIncorrectHeaderParamArrayNumber(t *testing.T) {
param := createMockParameter()
param.Name = "bubbles"

err := IncorrectHeaderParamArrayNumber(param, "milky", highSchema, nil)
err := IncorrectHeaderParamArrayNumber(param, "milky", highSchema, nil, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -914,7 +914,7 @@ func TestIncorrectPathParamBool(t *testing.T) {
param.Schema = base.CreateSchemaProxy(highSchema)
param.GoLow().Schema.KeyNode = &yaml.Node{}

err := IncorrectPathParamBool(param, "milky", highSchema)
err := IncorrectPathParamBool(param, "milky", highSchema, "/test-path", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down Expand Up @@ -945,7 +945,7 @@ items:
}
param.GoLow().Schema.Value.Schema().Enum.KeyNode = &yaml.Node{}

err := IncorrectPathParamEnum(param, "milky", highSchema)
err := IncorrectPathParamEnum(param, "milky", highSchema, "/test-path", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -970,7 +970,7 @@ func TestIncorrectPathParamNumber(t *testing.T) {
param.Schema = base.CreateSchemaProxy(highSchema)
param.GoLow().Schema.KeyNode = &yaml.Node{}

err := IncorrectPathParamNumber(param, "milky", highSchema)
err := IncorrectPathParamNumber(param, "milky", highSchema, "/test-path", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -995,7 +995,7 @@ func TestIncorrectPathParamInteger(t *testing.T) {
param.Schema = base.CreateSchemaProxy(highSchema)
param.GoLow().Schema.KeyNode = &yaml.Node{}

err := IncorrectPathParamInteger(param, "milky", highSchema)
err := IncorrectPathParamInteger(param, "milky", highSchema, "/test-path", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -1021,7 +1021,7 @@ func TestIncorrectPathParamArrayNumber(t *testing.T) {
param := createMockParameter()
param.Name = "bubbles"

err := IncorrectPathParamArrayNumber(param, "milky", highSchema, nil)
err := IncorrectPathParamArrayNumber(param, "milky", highSchema, nil, "/test-path", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -1047,7 +1047,7 @@ func TestIncorrectPathParamArrayInteger(t *testing.T) {
param := createMockParameter()
param.Name = "bubbles"

err := IncorrectPathParamArrayInteger(param, "milky", highSchema, nil)
err := IncorrectPathParamArrayInteger(param, "milky", highSchema, nil, "/test-path", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -1073,7 +1073,7 @@ func TestIncorrectPathParamArrayBoolean(t *testing.T) {
param := createMockParameter()
param.Name = "bubbles"

err := IncorrectPathParamArrayBoolean(param, "milky", highSchema, nil)
err := IncorrectPathParamArrayBoolean(param, "milky", highSchema, nil, "/test-path", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -1098,7 +1098,7 @@ func TestPathParameterMissing(t *testing.T) {
param.Schema = base.CreateSchemaProxy(highSchema)
param.GoLow().Schema.KeyNode = &yaml.Node{}

err := PathParameterMissing(param)
err := PathParameterMissing(param, "/test/{testQueryParam}", "/test/")

// Validate the error
require.NotNil(t, err)
Expand All @@ -1124,7 +1124,7 @@ items:
param.Schema = base.CreateSchemaProxy(highSchema)
param.GoLow().Schema.KeyNode = &yaml.Node{}

err := IncorrectParamArrayMaxNumItems(param, param.Schema.Schema(), 10, 25)
err := IncorrectParamArrayMaxNumItems(param, param.Schema.Schema(), 10, 25, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -1150,7 +1150,7 @@ items:
param.Schema = base.CreateSchemaProxy(highSchema)
param.GoLow().Schema.KeyNode = &yaml.Node{}

err := IncorrectParamArrayMinNumItems(param, param.Schema.Schema(), 10, 5)
err := IncorrectParamArrayMinNumItems(param, param.Schema.Schema(), 10, 5, "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand All @@ -1176,7 +1176,7 @@ items:
param.Schema = base.CreateSchemaProxy(highSchema)
param.GoLow().Schema.KeyNode = &yaml.Node{}

err := IncorrectParamArrayUniqueItems(param, param.Schema.Schema(), "fish, cake")
err := IncorrectParamArrayUniqueItems(param, param.Schema.Schema(), "fish, cake", "/test-path", "get", "{}")

// Validate the error
require.NotNil(t, err)
Expand Down
17 changes: 6 additions & 11 deletions errors/validation_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,10 @@ type SchemaValidationFailure struct {
// FieldPath is the JSONPath representation of the field location that failed validation (e.g., "$.user.email")
FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`

// KeywordLocation is the relative path to the JsonSchema keyword that failed validation
// This will be empty if the validation failure did not originate from JSON Schema validation
// KeywordLocation is the JSON Pointer (RFC 6901) path to the schema keyword that failed validation
// (e.g., "/properties/age/minimum")
KeywordLocation string `json:"keywordLocation,omitempty" yaml:"keywordLocation,omitempty"`

// AbsoluteKeywordLocation is the absolute path to the validation failure as exposed by the jsonschema library.
// This will be empty if the validation failure did not originate from JSON Schema validation
AbsoluteKeywordLocation string `json:"absoluteKeywordLocation,omitempty" yaml:"absoluteKeywordLocation,omitempty"`

// Line is the line number where the violation occurred. This may a local line number
// if the validation is a schema (only schemas are validated locally, so the line number will be relative to
// the Context object held by the ValidationError object).
Expand All @@ -54,15 +50,14 @@ type SchemaValidationFailure struct {

// The original jsonschema.ValidationError object, if the schema failure originated from the jsonschema library.
OriginalJsonSchemaError *jsonschema.ValidationError `json:"-" yaml:"-"`

// DEPRECATED in favor of explicit use of FieldPath & InstancePath
// Location is the XPath-like location of the validation failure
Location string `json:"location,omitempty" yaml:"location,omitempty"`
}

// Error returns a string representation of the error
func (s *SchemaValidationFailure) Error() string {
return fmt.Sprintf("Reason: %s, Location: %s", s.Reason, s.Location)
if s.FieldPath != "" {
return fmt.Sprintf("Reason: %s, FieldPath: %s", s.Reason, s.FieldPath)
}
return fmt.Sprintf("Reason: %s", s.Reason)
}

// ValidationError is a struct that contains all the information about a validation error.
Expand Down
Loading