Skip to content

Commit

Permalink
Fixes #72: support less restrictive equality comparison by the In rule
Browse files Browse the repository at this point in the history
  • Loading branch information
qiangxue committed Dec 5, 2019
1 parent d621af5 commit 094faa1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
10 changes: 7 additions & 3 deletions in.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}
}
Expand Down
1 change: 1 addition & 0 deletions in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 094faa1

Please sign in to comment.