Skip to content

Commit 8165c43

Browse files
authored
feat: formatting some error messages (#684)
1 parent 83dd2ff commit 8165c43

File tree

7 files changed

+24
-24
lines changed

7 files changed

+24
-24
lines changed

openapi3/issue136_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ components:
3131
},
3232
{
3333
dflt: `1`,
34-
err: "invalid components: invalid schema default: Field must be set to string or not be present",
34+
err: "invalid components: invalid schema default: field must be set to string or not be present",
3535
},
3636
} {
3737
t.Run(testcase.dflt, func(t *testing.T) {

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func Example() {
8181
// Output:
8282
// ===== Start New Error =====
8383
// @body.name:
84-
// Error at "/name": Field must be set to string or not be present
84+
// Error at "/name": field must be set to string or not be present
8585
// Schema:
8686
// {
8787
// "example": "doggie",
@@ -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: 14 additions & 14 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
},
@@ -379,13 +379,13 @@ func getValidationTests(t *testing.T) []*validationTest {
379379
bytes.NewBufferString(`{"name":"Bahama","photoUrls":"http://cat"}`)),
380380
},
381381
wantErrReason: "doesn't match the schema",
382-
wantErrSchemaReason: "Field must be set to array or not be present",
382+
wantErrSchemaReason: "field must be set to array or not be present",
383383
wantErrSchemaPath: "/photoUrls",
384384
wantErrSchemaValue: "string",
385385
// TODO: this shouldn't say "or not be present", but this requires recursively resolving
386386
// innerErr.JSONPointer() against e.RequestBody.Content["application/json"].Schema.Value (.Required, .Properties)
387387
wantErrResponse: &ValidationError{Status: http.StatusUnprocessableEntity,
388-
Title: "Field must be set to array or not be present",
388+
Title: "field must be set to array or not be present",
389389
Source: &ValidationErrorSource{Pointer: "/photoUrls"}},
390390
},
391391
{
@@ -659,7 +659,7 @@ func TestValidationHandler_ServeHTTP(t *testing.T) {
659659
body, err := ioutil.ReadAll(resp.Body)
660660
require.NoError(t, err)
661661
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
662-
require.Equal(t, "[422][][] Field must be set to array or not be present [source pointer=/photoUrls]", string(body))
662+
require.Equal(t, "[422][][] field must be set to array or not be present [source pointer=/photoUrls]", string(body))
663663
})
664664
}
665665

@@ -701,6 +701,6 @@ func TestValidationHandler_Middleware(t *testing.T) {
701701
body, err := ioutil.ReadAll(resp.Body)
702702
require.NoError(t, err)
703703
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
704-
require.Equal(t, "[422][][] Field must be set to array or not be present [source pointer=/photoUrls]", string(body))
704+
require.Equal(t, "[422][][] field must be set to array or not be present [source pointer=/photoUrls]", string(body))
705705
})
706706
}

routers/gorillamux/example_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Example() {
5353
err = openapi3filter.ValidateResponse(ctx, responseValidationInput)
5454
fmt.Println(err)
5555
// Output:
56-
// response body doesn't match the schema: Field must be set to string or not be present
56+
// response body doesn't match the schema: field must be set to string or not be present
5757
// Schema:
5858
// {
5959
// "type": "string"

0 commit comments

Comments
 (0)