Skip to content

Commit d9923d4

Browse files
authored
Merge pull request #200 from mjbonifacio/unify-context-and-centralize
Improved Error Reporting Consistency - SchemaValidationFailures are consistently returned, and error mapping information is consistently mapped
2 parents b8be188 + e7cbb6e commit d9923d4

37 files changed

Lines changed: 1153 additions & 513 deletions

errors/parameter_errors.go

Lines changed: 406 additions & 37 deletions
Large diffs are not rendered by default.

errors/parameter_errors_test.go

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func TestQueryParameterMissing(t *testing.T) {
122122
param := createMockParameterWithSchema()
123123

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

127127
// Validate the error
128128
require.NotNil(t, err)
@@ -138,7 +138,7 @@ func TestHeaderParameterMissing(t *testing.T) {
138138
param := createMockParameterWithSchema()
139139

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

143143
// Validate the error
144144
require.NotNil(t, err)
@@ -154,7 +154,7 @@ func TestCookieParameterMissing(t *testing.T) {
154154
param := createMockParameterWithSchema()
155155

156156
// Call the function
157-
err := CookieParameterMissing(param)
157+
err := CookieParameterMissing(param, "/test", "get", "")
158158

159159
// Validate the error
160160
require.NotNil(t, err)
@@ -171,7 +171,7 @@ func TestHeaderParameterCannotBeDecoded(t *testing.T) {
171171
val := "malformed_header_value"
172172

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

176176
// Validate the error
177177
require.NotNil(t, err)
@@ -209,7 +209,7 @@ func TestIncorrectHeaderParamEnum(t *testing.T) {
209209
schema := base.NewSchema(s)
210210

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

214214
// Validate the error
215215
require.NotNil(t, err)
@@ -246,7 +246,7 @@ func TestIncorrectQueryParamArrayBoolean(t *testing.T) {
246246
schema := base.NewSchema(s)
247247

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

251251
// Validate the error
252252
require.NotNil(t, err)
@@ -339,7 +339,7 @@ func TestIncorrectCookieParamArrayBoolean(t *testing.T) {
339339
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())
340340

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

344344
// Validate the error
345345
require.NotNil(t, err)
@@ -401,7 +401,7 @@ func TestIncorrectQueryParamArrayInteger(t *testing.T) {
401401
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())
402402

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

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

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

426426
// Validate the error
427427
require.NotNil(t, err)
@@ -483,7 +483,7 @@ func TestIncorrectCookieParamArrayNumber(t *testing.T) {
483483
itemsSchema := base.NewSchema(baseSchema.Items.Value.A.Schema())
484484

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

488488
// Validate the error
489489
require.NotNil(t, err)
@@ -560,7 +560,7 @@ func TestIncorrectParamEncodingJSON(t *testing.T) {
560560
baseSchema := createMockLowBaseSchema()
561561

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

565565
// Validate the error
566566
require.NotNil(t, err)
@@ -590,7 +590,7 @@ func TestIncorrectQueryParamBool(t *testing.T) {
590590
})
591591

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

595595
// Validate the error
596596
require.NotNil(t, err)
@@ -607,7 +607,7 @@ func TestInvalidQueryParamNumber(t *testing.T) {
607607
baseSchema := createMockLowBaseSchema()
608608

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

612612
// Validate the error
613613
require.NotNil(t, err)
@@ -624,7 +624,7 @@ func TestInvalidQueryParamInteger(t *testing.T) {
624624
baseSchema := createMockLowBaseSchema()
625625

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

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

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

655655
// Validate the error
656656
require.NotNil(t, err)
@@ -680,7 +680,7 @@ func TestIncorrectQueryParamEnumArray(t *testing.T) {
680680
}
681681

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

685685
// Validate the error
686686
require.NotNil(t, err)
@@ -704,7 +704,7 @@ func TestIncorrectReservedValues(t *testing.T) {
704704
param := createMockParameter()
705705
param.Name = "borked::?^&*"
706706

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

709709
// Validate the error
710710
require.NotNil(t, err)
@@ -728,7 +728,7 @@ func TestInvalidHeaderParamInteger(t *testing.T) {
728728
param := createMockParameter()
729729
param.Name = "bunny"
730730

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

733733
// Validate the error
734734
require.NotNil(t, err)
@@ -752,7 +752,7 @@ func TestInvalidHeaderParamNumber(t *testing.T) {
752752
param := createMockParameter()
753753
param.Name = "bunny"
754754

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

757757
// Validate the error
758758
require.NotNil(t, err)
@@ -776,7 +776,7 @@ func TestInvalidCookieParamNumber(t *testing.T) {
776776
param := createMockParameter()
777777
param.Name = "cookies"
778778

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

781781
// Validate the error
782782
require.NotNil(t, err)
@@ -800,7 +800,7 @@ func TestInvalidCookieParamInteger(t *testing.T) {
800800
param := createMockParameter()
801801
param.Name = "cookies"
802802

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

805805
// Validate the error
806806
require.NotNil(t, err)
@@ -824,7 +824,7 @@ func TestIncorrectHeaderParamBool(t *testing.T) {
824824
param := createMockParameter()
825825
param.Name = "cookies"
826826

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

829829
// Validate the error
830830
require.NotNil(t, err)
@@ -848,7 +848,7 @@ func TestIncorrectCookieParamBool(t *testing.T) {
848848
param := createMockParameter()
849849
param.Name = "cookies"
850850

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

853853
// Validate the error
854854
require.NotNil(t, err)
@@ -879,7 +879,7 @@ items:
879879
}
880880
param.GoLow().Schema.Value.Schema().Enum.KeyNode = &yaml.Node{}
881881

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

884884
// Validate the error
885885
require.NotNil(t, err)
@@ -906,7 +906,7 @@ func TestIncorrectHeaderParamArrayBoolean(t *testing.T) {
906906
param := createMockParameter()
907907
param.Name = "bubbles"
908908

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

911911
// Validate the error
912912
require.NotNil(t, err)
@@ -933,7 +933,7 @@ func TestIncorrectHeaderParamArrayNumber(t *testing.T) {
933933
param := createMockParameter()
934934
param.Name = "bubbles"
935935

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

938938
// Validate the error
939939
require.NotNil(t, err)
@@ -959,7 +959,7 @@ func TestIncorrectPathParamBool(t *testing.T) {
959959
param.Schema = base.CreateSchemaProxy(highSchema)
960960
param.GoLow().Schema.KeyNode = &yaml.Node{}
961961

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

964964
// Validate the error
965965
require.NotNil(t, err)
@@ -991,7 +991,7 @@ items:
991991
}
992992
param.GoLow().Schema.Value.Schema().Enum.KeyNode = &yaml.Node{}
993993

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

996996
// Validate the error
997997
require.NotNil(t, err)
@@ -1017,7 +1017,7 @@ func TestIncorrectPathParamNumber(t *testing.T) {
10171017
param.Schema = base.CreateSchemaProxy(highSchema)
10181018
param.GoLow().Schema.KeyNode = &yaml.Node{}
10191019

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

10221022
// Validate the error
10231023
require.NotNil(t, err)
@@ -1043,7 +1043,7 @@ func TestIncorrectPathParamInteger(t *testing.T) {
10431043
param.Schema = base.CreateSchemaProxy(highSchema)
10441044
param.GoLow().Schema.KeyNode = &yaml.Node{}
10451045

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

10481048
// Validate the error
10491049
require.NotNil(t, err)
@@ -1070,7 +1070,7 @@ func TestIncorrectPathParamArrayNumber(t *testing.T) {
10701070
param := createMockParameter()
10711071
param.Name = "bubbles"
10721072

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

10751075
// Validate the error
10761076
require.NotNil(t, err)
@@ -1097,7 +1097,7 @@ func TestIncorrectPathParamArrayInteger(t *testing.T) {
10971097
param := createMockParameter()
10981098
param.Name = "bubbles"
10991099

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

11021102
// Validate the error
11031103
require.NotNil(t, err)
@@ -1124,7 +1124,7 @@ func TestIncorrectPathParamArrayBoolean(t *testing.T) {
11241124
param := createMockParameter()
11251125
param.Name = "bubbles"
11261126

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

11291129
// Validate the error
11301130
require.NotNil(t, err)
@@ -1150,7 +1150,7 @@ func TestPathParameterMissing(t *testing.T) {
11501150
param.Schema = base.CreateSchemaProxy(highSchema)
11511151
param.GoLow().Schema.KeyNode = &yaml.Node{}
11521152

1153-
err := PathParameterMissing(param)
1153+
err := PathParameterMissing(param, "/test/{testQueryParam}", "/test/")
11541154

11551155
// Validate the error
11561156
require.NotNil(t, err)
@@ -1177,7 +1177,7 @@ items:
11771177
param.Schema = base.CreateSchemaProxy(highSchema)
11781178
param.GoLow().Schema.KeyNode = &yaml.Node{}
11791179

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

11821182
// Validate the error
11831183
require.NotNil(t, err)
@@ -1204,7 +1204,7 @@ items:
12041204
param.Schema = base.CreateSchemaProxy(highSchema)
12051205
param.GoLow().Schema.KeyNode = &yaml.Node{}
12061206

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

12091209
// Validate the error
12101210
require.NotNil(t, err)
@@ -1231,7 +1231,7 @@ items:
12311231
param.Schema = base.CreateSchemaProxy(highSchema)
12321232
param.GoLow().Schema.KeyNode = &yaml.Node{}
12331233

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

12361236
// Validate the error
12371237
require.NotNil(t, err)

errors/urlencoded_errors.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ func InvalidURLEncodedParsing(reason, referenceObject string) *ValidationError {
1515
Reason: fmt.Sprintf("failed to parse form-urlencoded: %s", reason),
1616
SchemaValidationErrors: []*SchemaValidationFailure{{
1717
Reason: reason,
18-
Location: "url encoded parsing",
1918
ReferenceSchema: "",
2019
ReferenceObject: referenceObject,
2120
}},

errors/urlencoded_errors_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ func TestInvalidURLEncodedParsing(t *testing.T) {
3838
err := InvalidURLEncodedParsing("no data sent", "invalid-formdata")
3939

4040
assert.NotNil(t, (*err))
41-
assert.Equal(t, (*err).SchemaValidationErrors[0].Location, "url encoded parsing")
41+
assert.Equal(t, (*err).SchemaValidationErrors[0].Reason, "no data sent")
42+
assert.Equal(t, (*err).SchemaValidationErrors[0].ReferenceObject, "invalid-formdata")
4243
assert.Equal(t, helpers.Schema, (*err).ValidationSubType)
4344
}
4445

0 commit comments

Comments
 (0)