From dfc652373a967ddc47bcdc3d992f097a34c91738 Mon Sep 17 00:00:00 2001 From: "Dmitry M. Gavrovsky" Date: Tue, 19 Nov 2019 21:00:18 +0200 Subject: [PATCH] =?UTF-8?q?Added=20boolean=20type=20support=20for=20?= =?UTF-8?q?=E2=80=9Ceq=E2=80=9D=20validator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- baked_in.go | 5 +++++ util.go | 10 ++++++++++ 2 files changed, 15 insertions(+) diff --git a/baked_in.go b/baked_in.go index 23f76b3..f80c315 100644 --- a/baked_in.go +++ b/baked_in.go @@ -1106,6 +1106,11 @@ func isEq(fl FieldLevel) bool { p := asFloat(param) return field.Float() == p + + case reflect.Bool: + p := asBool(param) + + return field.Bool() == p } panic(fmt.Sprintf("Bad field type %T", field.Interface())) diff --git a/util.go b/util.go index 71acbdc..0d3a181 100644 --- a/util.go +++ b/util.go @@ -249,6 +249,16 @@ func asFloat(param string) float64 { return i } +// asBool returns the parameter as a bool +// or panics if it can't convert +func asBool(param string) bool { + + i, err := strconv.ParseBool(param) + panicIf(err) + + return i +} + func panicIf(err error) { if err != nil { panic(err.Error())