Commit 72cc17b4 authored by Clayton Coleman's avatar Clayton Coleman

Omit empty quotes in message when required value field error type

parent c6a2e574
......@@ -84,13 +84,29 @@ type ValidationError struct {
var _ error = &ValidationError{}
func (v *ValidationError) Error() string {
s := spew.Sprintf("%s: %s '%+v'", v.Field, v.Type, v.BadValue)
if v.Detail != "" {
var s string
if v.Type == ValidationErrorTypeRequired && isEmpty(v.BadValue) {
s = spew.Sprintf("%s: %s", v.Field, v.Type)
} else {
s = spew.Sprintf("%s: %s '%+v'", v.Field, v.Type, v.BadValue)
}
if len(v.Detail) != 0 {
s += fmt.Sprintf(": %s", v.Detail)
}
return s
}
func isEmpty(obj interface{}) bool {
if obj == nil {
return true
}
switch t := obj.(type) {
case string:
return len(t) == 0
}
return false
}
// NewFieldRequired returns a *ValidationError indicating "value required"
func NewFieldRequired(field string, value interface{}) *ValidationError {
return &ValidationError{ValidationErrorTypeRequired, field, value, ""}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment