From 3a8d6360e6523df86917fa8ec085113765583b65 Mon Sep 17 00:00:00 2001 From: Faizan Khalid Date: Thu, 10 Jun 2021 12:09:48 +0500 Subject: [PATCH 1/3] Added in tag --- builtins.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ validator.go | 1 + 2 files changed, 65 insertions(+) diff --git a/builtins.go b/builtins.go index 6fdb92b..b02bdde 100644 --- a/builtins.go +++ b/builtins.go @@ -20,6 +20,7 @@ import ( "reflect" "regexp" "strconv" + "strings" "unicode/utf8" ) @@ -287,3 +288,66 @@ func nonnil(v interface{}, param string) error { } return nil } + +// in validates that the given value exists in list of given values +func in(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 +} diff --git a/validator.go b/validator.go index 4d610be..93e587d 100644 --- a/validator.go +++ b/validator.go @@ -140,6 +140,7 @@ func NewValidator() *Validator { "max": max, "regexp": regex, "nonnil": nonnil, + "in": in, }, printJSON: false, } From e01a7635c5ef94cd135c4ca130fa76c9ba2372fa Mon Sep 17 00:00:00 2001 From: Faizan Khalid Date: Thu, 10 Jun 2021 12:18:59 +0500 Subject: [PATCH 2/3] Updated README --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index ef9ada9..9783a80 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,13 @@ regexp nonnil Validates that the given value is not nil. (Usage: nonnil) + +in + 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: in=[FOO,BAR,BAZ]) ``` Custom validators From d70861b3d479e14f5d0bc764760f02a73631ca4d Mon Sep 17 00:00:00 2001 From: Faizan Khalid Date: Thu, 10 Jun 2021 12:21:21 +0500 Subject: [PATCH 3/3] Renamed validator in to list --- README.md | 4 ++-- builtins.go | 4 ++-- validator.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 9783a80..7288c25 100644 --- a/README.md +++ b/README.md @@ -87,12 +87,12 @@ regexp nonnil Validates that the given value is not nil. (Usage: nonnil) -in +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: in=[FOO,BAR,BAZ]) + point numbers. (Usage: list=[FOO,BAR,BAZ]) ``` Custom validators diff --git a/builtins.go b/builtins.go index b02bdde..c257ea7 100644 --- a/builtins.go +++ b/builtins.go @@ -289,8 +289,8 @@ func nonnil(v interface{}, param string) error { return nil } -// in validates that the given value exists in list of given values -func in(v interface{}, param string) error { +// 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() { diff --git a/validator.go b/validator.go index 93e587d..bc95d37 100644 --- a/validator.go +++ b/validator.go @@ -140,7 +140,7 @@ func NewValidator() *Validator { "max": max, "regexp": regex, "nonnil": nonnil, - "in": in, + "list": list, }, printJSON: false, }