From 094faa164a0dbeb9a9062ed57514e85fa3c66c04 Mon Sep 17 00:00:00 2001 From: qiangxue Date: Thu, 5 Dec 2019 06:52:38 -0500 Subject: [PATCH] Fixes #72: support less restrictive equality comparison by the In rule --- in.go | 10 +++++++--- in_test.go | 1 + 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/in.go b/in.go index 7adf282..40b7502 100644 --- a/in.go +++ b/in.go @@ -4,10 +4,14 @@ package validation -import "errors" +import ( + "errors" + "reflect" +) // In returns a validation rule that checks if a value can be found in the given list of values. -// Note that the value being checked and the possible range of values must be of the same type. +// reflect.DeepEqual() will be used to determine if two values are equal. +// For more details please refer to https://golang.org/pkg/reflect/#DeepEqual // An empty value is considered valid. Use the Required rule to make sure a value is not empty. func In(values ...interface{}) InRule { return InRule{ @@ -30,7 +34,7 @@ func (r InRule) Validate(value interface{}) error { } for _, e := range r.elements { - if e == value { + if reflect.DeepEqual(e, value) { return nil } } diff --git a/in_test.go b/in_test.go index 26aae6b..31642ef 100644 --- a/in_test.go +++ b/in_test.go @@ -27,6 +27,7 @@ func TestIn(t *testing.T) { {"t5", []interface{}{1, 2}, "1", "must be a valid value"}, {"t6", []interface{}{1, 2}, &v, ""}, {"t7", []interface{}{1, 2}, v2, ""}, + {"t8", []interface{}{[]byte{1}, 1, 2}, []byte{1}, ""}, } for _, test := range tests {