Validate enums (finally).

This commit is contained in:
Alec Thomas
2018-09-23 19:59:25 +10:00
parent d648254c7d
commit 5d7703774f
3 changed files with 24 additions and 0 deletions
+11
View File
@@ -119,6 +119,17 @@ func (c *Context) Empty() bool {
// Validate the current context.
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.Summary(), value.Enum, value.Target.Interface())
}
}
return next(nil)
})
if err != nil {
return err
}
for _, resolver := range c.combineResolvers() {
if err := resolver.Validate(c.Model); err != nil {
return err