- commit
- 5175718db6359d58fc4293742d81010a3d570d91
- parent
- 358004850b5244298ce85ec35ef2dfb2368a67a0
- author
- Dami <dami.etoile@gmail.com>
| M | internal/ | 44 | +++++++++++++++++++++++++++--- |
@@ -9,6 +9,7 @@ import (
99 "net/http"1010 "regexp"1111 "sort"12+ "strconv"1213 "strings"1314 )1415 @@ -168,11 +169,8 @@ func validate(schema map[string]any, value any, path string) error {
168169 return fmt.Errorf("%s %s", where(), err)169170 }170171 }171- if options, ok := schema["enum"].([]string); ok {172- text, _ := value.(string)173- if !contains(options, text) {174- return fmt.Errorf("%s must be one of %s", where(), strings.Join(options, ", "))175- }172+ if options := enumOptions(schema["enum"]); len(options) > 0 && !contains(options, scalar(value)) {173+ return fmt.Errorf("%s must be one of %s", where(), strings.Join(options, ", "))176174 }177175 if pattern, ok := schema["pattern"].(string); ok {178176 text, _ := value.(string)@@ -286,6 +284,42 @@ func required(schema map[string]any) []string {
286284 return nil287285 }288286 287+// enumOptions renders enum members as strings so integer and string enums288+// compare the same way.289+func enumOptions(value any) []string {290+ switch list := value.(type) {291+ case []string:292+ return list293+ case []int:294+ out := make([]string, 0, len(list))295+ for _, item := range list {296+ out = append(out, strconv.Itoa(item))297+ }298+ return out299+ case []any:300+ out := make([]string, 0, len(list))301+ for _, item := range list {302+ out = append(out, scalar(item))303+ }304+ return out305+ }306+ return nil307+}308+309+func scalar(value any) string {310+ switch v := value.(type) {311+ case string:312+ return v313+ case float64:314+ return strconv.FormatFloat(v, 'f', -1, 64)315+ case int:316+ return strconv.Itoa(v)317+ case bool:318+ return strconv.FormatBool(v)319+ }320+ return ""321+}322+289323 func number(value any) (float64, bool) {290324 switch n := value.(type) {291325 case int: