Quote enum values when displaying errors.

This commit is contained in:
Alec Thomas
2019-06-21 14:51:44 +10:00
parent 102a975844
commit 33b8d2b19c
2 changed files with 10 additions and 4 deletions
+7 -1
View File
@@ -3,6 +3,7 @@ package kong
import (
"fmt"
"reflect"
"sort"
"strconv"
"strings"
@@ -135,7 +136,12 @@ func (c *Context) Validate() error {
err := Visit(c.Model, func(node Visitable, next Next) error {
if value, ok := node.(*Value); ok {
if value.Enum != "" && !value.EnumMap()[fmt.Sprintf("%v", value.Target.Interface())] {
return fmt.Errorf("%s must be one of %s but got %q", value.ShortSummary(), value.Enum, value.Target.Interface())
enums := []string{}
for enum := range value.EnumMap() {
enums = append(enums, fmt.Sprintf("%q", enum))
}
sort.Strings(enums)
return fmt.Errorf("%s must be one of %s but got %q", value.ShortSummary(), strings.Join(enums, ","), value.Target.Interface())
}
}
return next(nil)