Skip to content

Commit

Permalink
feat: formatting some error messages
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
eloycoto committed Nov 28, 2022
1 parent 83dd2ff commit 2511d1f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
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
2 changes: 1 addition & 1 deletion openapi3filter/unpack_errors_test.go
Expand Up @@ -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
20 changes: 10 additions & 10 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

0 comments on commit 2511d1f

Please sign in to comment.