Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added list tag #98

Open
wants to merge 3 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ regexp

nonnil
Validates that the given value is not nil. (Usage: nonnil)

list
Validates that the given value is present in a list of
provided values. These values should be provided inside
square brackets separated by comma without any additional
spaces. It can be used with strings, integers and floating
point numbers. (Usage: list=[FOO,BAR,BAZ])
```

Custom validators
Expand Down
64 changes: 64 additions & 0 deletions builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"reflect"
"regexp"
"strconv"
"strings"
"unicode/utf8"
)

Expand Down Expand Up @@ -287,3 +288,66 @@ func nonnil(v interface{}, param string) error {
}
return nil
}

// list validates that the given value exists in the list of values
func list(v interface{}, param string) error {
value := reflect.ValueOf(v)
if value.Kind() == reflect.Ptr {
if value.IsNil() {
return nil
}
value = value.Elem()
}

// get array of possible values
l := len(param)
if l < 3 || param[0] != '[' || param[l-1] != ']' {
return ErrBadParameter
}
param = param[1 : l-1]

possible := strings.Split(param, ",")

switch value.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
for _, str := range possible {
i, err := strconv.ParseInt(str, 10, 64)
if err != nil {
return ErrBadParameter
}
if value.Int() == i {
return nil
}
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
for _, str := range possible {
i, err := strconv.ParseUint(str, 10, 64)
if err != nil {
return ErrBadParameter
}
if value.Uint() == i {
return nil
}
}
case reflect.Float32, reflect.Float64:
for _, str := range possible {
f, err := strconv.ParseFloat(str, 64)
if err != nil {
return ErrBadParameter
}
if value.Float() == f {
return nil
}
}
case reflect.String:
for _, str := range possible {
if value.String() == str {
return nil
}
}
default:
return ErrUnsupported
}

return ErrInvalid
}
1 change: 1 addition & 0 deletions validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ func NewValidator() *Validator {
"max": max,
"regexp": regex,
"nonnil": nonnil,
"list": list,
},
printJSON: false,
}
Expand Down