forked from Antipitch/orwell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
in.go
50 lines (44 loc) · 827 Bytes
/
in.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package orwell
import (
"fmt"
)
// In func
func (*Orwell) In(args ...interface{}) *in {
return &in{
args: args,
msg: fmt.Sprintf("Validation error for 'In' rule"),
}
}
// In struct
type in struct {
args []interface{}
msg string
}
// Apply rule
func (r *in) Apply(value interface{}) error {
v, isNil := IsNil(value)
if isNil || IsEmpty(v) {
return nil
}
switch v.(type) {
case string:
vs, _ := ToString(v)
for _, arg := range r.args {
if as, err := ToString(arg); err == nil {
if vs == as {
return nil
}
}
}
case int, int8, int16, int32, int64:
vi, _ := ToInt64(v)
for _, arg := range r.args {
if ai, err := ToInt64(arg); err == nil {
if vi == ai {
return nil
}
}
}
}
return fmt.Errorf("%s: value and rule argument types are not comparable", r.msg)
}