Skip to content

Commit 2511d1f

Browse files
committed
feat: formatting some error messages
This commits adds some clarity to the end users about what it is failing on their end, adding values does not match, value X is not part of the enum, etc.. So for end-users it's a bit easier to understand. Signed-off-by: Eloy Coto <eloy.coto@acalustra.com>
1 parent 83dd2ff commit 2511d1f

File tree

5 files changed

+17
-17
lines changed

5 files changed

+17
-17
lines changed

openapi3/schema.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
883883
Value: value,
884884
Schema: schema,
885885
SchemaField: "enum",
886-
Reason: "value is not one of the allowed values",
886+
Reason: fmt.Sprintf("value %q is not one of the allowed values", value),
887887
customizeMessageError: settings.customizeMessageError,
888888
}
889889
}
@@ -1085,7 +1085,7 @@ func (schema *Schema) visitJSONNumber(settings *schemaValidationSettings, value
10851085
Value: value,
10861086
Schema: schema,
10871087
SchemaField: "type",
1088-
Reason: "Value must be an integer",
1088+
Reason: fmt.Sprintf("Value \"%g\" must be an integer", value),
10891089
customizeMessageError: settings.customizeMessageError,
10901090
}
10911091
if !settings.multiError {
@@ -1305,7 +1305,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
13051305
Value: value,
13061306
Schema: schema,
13071307
SchemaField: "pattern",
1308-
Reason: fmt.Sprintf(`string doesn't match the regular expression "%s"`, schema.Pattern),
1308+
Reason: fmt.Sprintf(`string %q doesn't match the regular expression "%s"`, value, schema.Pattern),
13091309
customizeMessageError: settings.customizeMessageError,
13101310
}
13111311
if !settings.multiError {
@@ -1650,7 +1650,7 @@ func (schema *Schema) expectedType(settings *schemaValidationSettings, typ strin
16501650
Value: typ,
16511651
Schema: schema,
16521652
SchemaField: "type",
1653-
Reason: "Field must be set to " + schema.Type + " or not be present",
1653+
Reason: fmt.Sprintf("Field must be set to %s or not be present", schema.Type),
16541654
customizeMessageError: settings.customizeMessageError,
16551655
}
16561656
}

openapi3filter/issue201_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ paths:
9494
},
9595

9696
"invalid required header": {
97-
err: `response header "X-Blup" doesn't match the schema: string doesn't match the regular expression "^blup$"`,
97+
err: `response header "X-Blup" doesn't match the schema: string "bluuuuuup" doesn't match the regular expression "^blup$"`,
9898
headers: map[string]string{
9999
"X-Blip": "blip",
100100
"x-blop": "blop",

openapi3filter/issue625_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ paths:
7272
name: "failed allof object array",
7373
spec: allOfArraySpec,
7474
req: `/items?test=1.2,3.1`,
75-
errStr: `parameter "test" in query has an error: Error at "/0": Value must be an integer`,
75+
errStr: `parameter "test" in query has an error: Error at "/0": Value "1.2" must be an integer`,
7676
},
7777
{
7878
name: "success oneof object array",

openapi3filter/unpack_errors_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func Example() {
9393
//
9494
// ===== Start New Error =====
9595
// @body.status:
96-
// Error at "/status": value is not one of the allowed values
96+
// Error at "/status": value "invalidStatus" is not one of the allowed values
9797
// Schema:
9898
// {
9999
// "description": "pet status in the store",

openapi3filter/validation_error_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -244,11 +244,11 @@ func getValidationTests(t *testing.T) []*validationTest {
244244
},
245245
wantErrParam: "status",
246246
wantErrParamIn: "query",
247-
wantErrSchemaReason: "value is not one of the allowed values",
247+
wantErrSchemaReason: "value \"available,sold\" is not one of the allowed values",
248248
wantErrSchemaPath: "/0",
249249
wantErrSchemaValue: "available,sold",
250250
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
251-
Title: "value is not one of the allowed values",
251+
Title: "value \"available,sold\" is not one of the allowed values",
252252
Detail: "value available,sold at /0 must be one of: available, pending, sold; " +
253253
// TODO: do we really want to use this heuristic to guess
254254
// that they're using the wrong serialization?
@@ -262,11 +262,11 @@ func getValidationTests(t *testing.T) []*validationTest {
262262
},
263263
wantErrParam: "status",
264264
wantErrParamIn: "query",
265-
wantErrSchemaReason: "value is not one of the allowed values",
265+
wantErrSchemaReason: "value \"watdis\" is not one of the allowed values",
266266
wantErrSchemaPath: "/1",
267267
wantErrSchemaValue: "watdis",
268268
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
269-
Title: "value is not one of the allowed values",
269+
Title: "value \"watdis\" is not one of the allowed values",
270270
Detail: "value watdis at /1 must be one of: available, pending, sold",
271271
Source: &ValidationErrorSource{Parameter: "status"}},
272272
},
@@ -278,11 +278,11 @@ func getValidationTests(t *testing.T) []*validationTest {
278278
},
279279
wantErrParam: "kind",
280280
wantErrParamIn: "query",
281-
wantErrSchemaReason: "value is not one of the allowed values",
281+
wantErrSchemaReason: "value \"fish,with,commas\" is not one of the allowed values",
282282
wantErrSchemaPath: "/1",
283283
wantErrSchemaValue: "fish,with,commas",
284284
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
285-
Title: "value is not one of the allowed values",
285+
Title: "value \"fish,with,commas\" is not one of the allowed values",
286286
Detail: "value fish,with,commas at /1 must be one of: dog, cat, turtle, bird,with,commas",
287287
// No 'perhaps you intended' because its the right serialization format
288288
Source: &ValidationErrorSource{Parameter: "kind"}},
@@ -304,11 +304,11 @@ func getValidationTests(t *testing.T) []*validationTest {
304304
},
305305
wantErrParam: "x-environment",
306306
wantErrParamIn: "header",
307-
wantErrSchemaReason: "value is not one of the allowed values",
307+
wantErrSchemaReason: "value \"watdis\" is not one of the allowed values",
308308
wantErrSchemaPath: "/",
309309
wantErrSchemaValue: "watdis",
310310
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
311-
Title: "value is not one of the allowed values",
311+
Title: "value \"watdis\" is not one of the allowed values",
312312
Detail: "value watdis at / must be one of: demo, prod",
313313
Source: &ValidationErrorSource{Parameter: "x-environment"}},
314314
},
@@ -323,11 +323,11 @@ func getValidationTests(t *testing.T) []*validationTest {
323323
r: newPetstoreRequest(t, http.MethodPost, "/pet", bytes.NewBufferString(`{"status":"watdis"}`)),
324324
},
325325
wantErrReason: "doesn't match the schema",
326-
wantErrSchemaReason: "value is not one of the allowed values",
326+
wantErrSchemaReason: "value \"watdis\" is not one of the allowed values",
327327
wantErrSchemaValue: "watdis",
328328
wantErrSchemaPath: "/status",
329329
wantErrResponse: &ValidationError{Status: http.StatusUnprocessableEntity,
330-
Title: "value is not one of the allowed values",
330+
Title: "value \"watdis\" is not one of the allowed values",
331331
Detail: "value watdis at /status must be one of: available, pending, sold",
332332
Source: &ValidationErrorSource{Pointer: "/status"}},
333333
},

0 commit comments

Comments
 (0)