Skip to content

Commit 094faa1

Browse files
committed
Fixes #72: support less restrictive equality comparison by the In rule
1 parent d621af5 commit 094faa1

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

in.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
package validation
66

7-
import "errors"
7+
import (
8+
"errors"
9+
"reflect"
10+
)
811

912
// In returns a validation rule that checks if a value can be found in the given list of values.
10-
// Note that the value being checked and the possible range of values must be of the same type.
13+
// reflect.DeepEqual() will be used to determine if two values are equal.
14+
// For more details please refer to https://golang.org/pkg/reflect/#DeepEqual
1115
// An empty value is considered valid. Use the Required rule to make sure a value is not empty.
1216
func In(values ...interface{}) InRule {
1317
return InRule{
@@ -30,7 +34,7 @@ func (r InRule) Validate(value interface{}) error {
3034
}
3135

3236
for _, e := range r.elements {
33-
if e == value {
37+
if reflect.DeepEqual(e, value) {
3438
return nil
3539
}
3640
}

in_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func TestIn(t *testing.T) {
2727
{"t5", []interface{}{1, 2}, "1", "must be a valid value"},
2828
{"t6", []interface{}{1, 2}, &v, ""},
2929
{"t7", []interface{}{1, 2}, v2, ""},
30+
{"t8", []interface{}{[]byte{1}, 1, 2}, []byte{1}, ""},
3031
}
3132

3233
for _, test := range tests {

0 commit comments

Comments
 (0)