Skip to content

Commit

Permalink
feat: formatting some error messages (#684)
Browse files Browse the repository at this point in the history
  • Loading branch information
eloycoto committed Nov 29, 2022
1 parent 83dd2ff commit 8165c43
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion openapi3/issue136_test.go
Expand Up @@ -31,7 +31,7 @@ components:
},
{
dflt: `1`,
err: "invalid components: invalid schema default: Field must be set to string or not be present",
err: "invalid components: invalid schema default: field must be set to string or not be present",
},
} {
t.Run(testcase.dflt, func(t *testing.T) {
Expand Down
8 changes: 4 additions & 4 deletions openapi3/schema.go
Expand Up @@ -883,7 +883,7 @@ func (schema *Schema) visitSetOperations(settings *schemaValidationSettings, val
Value: value,
Schema: schema,
SchemaField: "enum",
Reason: "value is not one of the allowed values",
Reason: fmt.Sprintf("value %q is not one of the allowed values", value),
customizeMessageError: settings.customizeMessageError,
}
}
Expand Down Expand Up @@ -1085,7 +1085,7 @@ func (schema *Schema) visitJSONNumber(settings *schemaValidationSettings, value
Value: value,
Schema: schema,
SchemaField: "type",
Reason: "Value must be an integer",
Reason: fmt.Sprintf("value \"%g\" must be an integer", value),
customizeMessageError: settings.customizeMessageError,
}
if !settings.multiError {
Expand Down Expand Up @@ -1305,7 +1305,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
Value: value,
Schema: schema,
SchemaField: "pattern",
Reason: fmt.Sprintf(`string doesn't match the regular expression "%s"`, schema.Pattern),
Reason: fmt.Sprintf(`string %q doesn't match the regular expression "%s"`, value, schema.Pattern),
customizeMessageError: settings.customizeMessageError,
}
if !settings.multiError {
Expand Down Expand Up @@ -1650,7 +1650,7 @@ func (schema *Schema) expectedType(settings *schemaValidationSettings, typ strin
Value: typ,
Schema: schema,
SchemaField: "type",
Reason: "Field must be set to " + schema.Type + " or not be present",
Reason: fmt.Sprintf("field must be set to %s or not be present", schema.Type),
customizeMessageError: settings.customizeMessageError,
}
}
Expand Down
2 changes: 1 addition & 1 deletion openapi3filter/issue201_test.go
Expand Up @@ -94,7 +94,7 @@ paths:
},

"invalid required header": {
err: `response header "X-Blup" doesn't match the schema: string doesn't match the regular expression "^blup$"`,
err: `response header "X-Blup" doesn't match the schema: string "bluuuuuup" doesn't match the regular expression "^blup$"`,
headers: map[string]string{
"X-Blip": "blip",
"x-blop": "blop",
Expand Down
2 changes: 1 addition & 1 deletion openapi3filter/issue625_test.go
Expand Up @@ -72,7 +72,7 @@ paths:
name: "failed allof object array",
spec: allOfArraySpec,
req: `/items?test=1.2,3.1`,
errStr: `parameter "test" in query has an error: Error at "/0": Value must be an integer`,
errStr: `parameter "test" in query has an error: Error at "/0": value "1.2" must be an integer`,
},
{
name: "success oneof object array",
Expand Down
4 changes: 2 additions & 2 deletions openapi3filter/unpack_errors_test.go
Expand Up @@ -81,7 +81,7 @@ func Example() {
// Output:
// ===== Start New Error =====
// @body.name:
// Error at "/name": Field must be set to string or not be present
// Error at "/name": field must be set to string or not be present
// Schema:
// {
// "example": "doggie",
Expand All @@ -93,7 +93,7 @@ func Example() {
//
// ===== Start New Error =====
// @body.status:
// Error at "/status": value is not one of the allowed values
// Error at "/status": value "invalidStatus" is not one of the allowed values
// Schema:
// {
// "description": "pet status in the store",
Expand Down
28 changes: 14 additions & 14 deletions openapi3filter/validation_error_test.go
Expand Up @@ -244,11 +244,11 @@ func getValidationTests(t *testing.T) []*validationTest {
},
wantErrParam: "status",
wantErrParamIn: "query",
wantErrSchemaReason: "value is not one of the allowed values",
wantErrSchemaReason: "value \"available,sold\" is not one of the allowed values",
wantErrSchemaPath: "/0",
wantErrSchemaValue: "available,sold",
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
Title: "value is not one of the allowed values",
Title: "value \"available,sold\" is not one of the allowed values",
Detail: "value available,sold at /0 must be one of: available, pending, sold; " +
// TODO: do we really want to use this heuristic to guess
// that they're using the wrong serialization?
Expand All @@ -262,11 +262,11 @@ func getValidationTests(t *testing.T) []*validationTest {
},
wantErrParam: "status",
wantErrParamIn: "query",
wantErrSchemaReason: "value is not one of the allowed values",
wantErrSchemaReason: "value \"watdis\" is not one of the allowed values",
wantErrSchemaPath: "/1",
wantErrSchemaValue: "watdis",
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
Title: "value is not one of the allowed values",
Title: "value \"watdis\" is not one of the allowed values",
Detail: "value watdis at /1 must be one of: available, pending, sold",
Source: &ValidationErrorSource{Parameter: "status"}},
},
Expand All @@ -278,11 +278,11 @@ func getValidationTests(t *testing.T) []*validationTest {
},
wantErrParam: "kind",
wantErrParamIn: "query",
wantErrSchemaReason: "value is not one of the allowed values",
wantErrSchemaReason: "value \"fish,with,commas\" is not one of the allowed values",
wantErrSchemaPath: "/1",
wantErrSchemaValue: "fish,with,commas",
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
Title: "value is not one of the allowed values",
Title: "value \"fish,with,commas\" is not one of the allowed values",
Detail: "value fish,with,commas at /1 must be one of: dog, cat, turtle, bird,with,commas",
// No 'perhaps you intended' because its the right serialization format
Source: &ValidationErrorSource{Parameter: "kind"}},
Expand All @@ -304,11 +304,11 @@ func getValidationTests(t *testing.T) []*validationTest {
},
wantErrParam: "x-environment",
wantErrParamIn: "header",
wantErrSchemaReason: "value is not one of the allowed values",
wantErrSchemaReason: "value \"watdis\" is not one of the allowed values",
wantErrSchemaPath: "/",
wantErrSchemaValue: "watdis",
wantErrResponse: &ValidationError{Status: http.StatusBadRequest,
Title: "value is not one of the allowed values",
Title: "value \"watdis\" is not one of the allowed values",
Detail: "value watdis at / must be one of: demo, prod",
Source: &ValidationErrorSource{Parameter: "x-environment"}},
},
Expand All @@ -323,11 +323,11 @@ func getValidationTests(t *testing.T) []*validationTest {
r: newPetstoreRequest(t, http.MethodPost, "/pet", bytes.NewBufferString(`{"status":"watdis"}`)),
},
wantErrReason: "doesn't match the schema",
wantErrSchemaReason: "value is not one of the allowed values",
wantErrSchemaReason: "value \"watdis\" is not one of the allowed values",
wantErrSchemaValue: "watdis",
wantErrSchemaPath: "/status",
wantErrResponse: &ValidationError{Status: http.StatusUnprocessableEntity,
Title: "value is not one of the allowed values",
Title: "value \"watdis\" is not one of the allowed values",
Detail: "value watdis at /status must be one of: available, pending, sold",
Source: &ValidationErrorSource{Pointer: "/status"}},
},
Expand Down Expand Up @@ -379,13 +379,13 @@ func getValidationTests(t *testing.T) []*validationTest {
bytes.NewBufferString(`{"name":"Bahama","photoUrls":"http://cat"}`)),
},
wantErrReason: "doesn't match the schema",
wantErrSchemaReason: "Field must be set to array or not be present",
wantErrSchemaReason: "field must be set to array or not be present",
wantErrSchemaPath: "/photoUrls",
wantErrSchemaValue: "string",
// TODO: this shouldn't say "or not be present", but this requires recursively resolving
// innerErr.JSONPointer() against e.RequestBody.Content["application/json"].Schema.Value (.Required, .Properties)
wantErrResponse: &ValidationError{Status: http.StatusUnprocessableEntity,
Title: "Field must be set to array or not be present",
Title: "field must be set to array or not be present",
Source: &ValidationErrorSource{Pointer: "/photoUrls"}},
},
{
Expand Down Expand Up @@ -659,7 +659,7 @@ func TestValidationHandler_ServeHTTP(t *testing.T) {
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Equal(t, "[422][][] Field must be set to array or not be present [source pointer=/photoUrls]", string(body))
require.Equal(t, "[422][][] field must be set to array or not be present [source pointer=/photoUrls]", string(body))
})
}

Expand Down Expand Up @@ -701,6 +701,6 @@ func TestValidationHandler_Middleware(t *testing.T) {
body, err := ioutil.ReadAll(resp.Body)
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Equal(t, "[422][][] Field must be set to array or not be present [source pointer=/photoUrls]", string(body))
require.Equal(t, "[422][][] field must be set to array or not be present [source pointer=/photoUrls]", string(body))
})
}
2 changes: 1 addition & 1 deletion routers/gorillamux/example_test.go
Expand Up @@ -53,7 +53,7 @@ func Example() {
err = openapi3filter.ValidateResponse(ctx, responseValidationInput)
fmt.Println(err)
// Output:
// response body doesn't match the schema: Field must be set to string or not be present
// response body doesn't match the schema: field must be set to string or not be present
// Schema:
// {
// "type": "string"
Expand Down

0 comments on commit 8165c43

Please sign in to comment.