forked from Antipitch/orwell
-
Notifications
You must be signed in to change notification settings - Fork 1
/
xgtor.go
51 lines (43 loc) · 739 Bytes
/
xgtor.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 orwell
import (
"fmt"
)
// XGtOr func
func (*Orwell) XGtOr(xv interface{}, gt int, ors ...interface{}) *xGtOr {
return &xGtOr{
xv: xv,
gt: gt,
ors: ors,
msg: fmt.Sprintf("Validation error for 'XGtOr' rule"),
}
}
// xGtOr struct
type xGtOr struct {
xv interface{}
gt int
ors []interface{}
msg string
}
// Apply rule
func (r *xGtOr) Apply(value interface{}) error {
if !NOE(value) {
return nil
}
xv, isNil := IsNil(r.xv)
if isNil || IsEmpty(xv) {
return nil
}
xvInt, err := ToInt64(xv)
if err != nil {
return fmt.Errorf("%s: %s", r.msg, err.Error())
}
if xvInt > int64(r.gt) {
for _, or := range r.ors {
if !NOE(or) {
return nil
}
}
return fmt.Errorf(r.msg)
}
return nil
}