-
Notifications
You must be signed in to change notification settings - Fork 0
/
multipleof.go
51 lines (44 loc) · 883 Bytes
/
multipleof.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
51
package validation
import (
"reflect"
)
func MultipleOf(threshold interface{}) *multipleOfRule {
return &multipleOfRule{
threshold,
1106,
}
}
type multipleOfRule struct {
threshold interface{}
code int
}
func (r *multipleOfRule) Validate(value interface{}) (code int, args []interface{}) {
rv := reflect.ValueOf(r.threshold)
args = append(args, rv.Kind())
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := ToInt(value)
if err != nil {
code = r.code
return
}
if v%rv.Int() == 0 {
code = r.code
return
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
v, err := ToUint(value)
if err != nil {
code = r.code
return
}
if v%rv.Uint() == 0 {
code = r.code
return
}
default:
code = r.code
return
}
return
}