mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-30 15:05:27 +00:00
Omit empty quotes in message when required value field error type
This commit is contained in:
parent
c6a2e574c2
commit
72cc17b41c
@ -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, ""}
|
||||
|
Loading…
Reference in New Issue
Block a user