summaryrefslogtreecommitdiff
path: root/vendor/github.com/go-playground/validator/v10/util.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/go-playground/validator/v10/util.go')
-rw-r--r--vendor/github.com/go-playground/validator/v10/util.go17
1 files changed, 17 insertions, 0 deletions
diff --git a/vendor/github.com/go-playground/validator/v10/util.go b/vendor/github.com/go-playground/validator/v10/util.go
index 4bd947bdf..16851593d 100644
--- a/vendor/github.com/go-playground/validator/v10/util.go
+++ b/vendor/github.com/go-playground/validator/v10/util.go
@@ -1,7 +1,9 @@
package validator
import (
+ "fmt"
"reflect"
+ "regexp"
"strconv"
"strings"
"time"
@@ -292,3 +294,18 @@ func panicIf(err error) {
panic(err.Error())
}
}
+
+// Checks if field value matches regex. If fl.Field can be cast to Stringer, it uses the Stringer interfaces
+// String() return value. Otherwise, it uses fl.Field's String() value.
+func fieldMatchesRegexByStringerValOrString(regex *regexp.Regexp, fl FieldLevel) bool {
+ switch fl.Field().Kind() {
+ case reflect.String:
+ return regex.MatchString(fl.Field().String())
+ default:
+ if stringer, ok := fl.Field().Interface().(fmt.Stringer); ok {
+ return regex.MatchString(stringer.String())
+ } else {
+ return regex.MatchString(fl.Field().String())
+ }
+ }
+}