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 3406b99
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 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(`value %q doesn't match the regular expression "%s"`, value, schema.Pattern),
customizeMessageError: settings.customizeMessageError,
}
if !settings.multiError {
Expand All @@ -1322,7 +1322,7 @@ func (schema *Schema) visitJSONString(settings *schemaValidationSettings, value
switch {
case f.regexp != nil && f.callback == nil:
if cp := f.regexp; !cp.MatchString(value) {
formatStrErr = fmt.Sprintf(`string doesn't match the format %q (regular expression "%s")`, format, cp.String())
formatStrErr = fmt.Sprintf(`value %q doesn't match the format %q (regular expression "%s")`, value, format, cp.String())
}
case f.regexp == nil && f.callback != nil:
if err := f.callback(value); err != nil {
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 %q or not be present", schema.Type),
customizeMessageError: settings.customizeMessageError,
}
}
Expand Down

0 comments on commit 3406b99

Please sign in to comment.