-
Notifications
You must be signed in to change notification settings - Fork 0
/
std.go
130 lines (115 loc) · 3.33 KB
/
std.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package validator
import (
"fmt"
"reflect"
)
func StdNone() (bool, string, bool) {
return true, "", Break
}
func StdOptional(v interface{}) (bool, string, bool) {
rv := reflect.ValueOf(v)
zv := reflect.Zero(rv.Type())
if reflect.DeepEqual(rv.Interface(), zv.Interface()) {
// it's a zero value, break the validation chain
return true, "", Break
}
return true, "", Continue
}
func StdEmpty(v interface{}) (bool, string) {
rv := reflect.ValueOf(v)
zv := reflect.Zero(rv.Type())
return reflect.DeepEqual(rv.Interface(), zv.Interface()), "should be empty"
}
func StdNonEmpty(v interface{}) (bool, string) {
rv := reflect.ValueOf(v)
zv := reflect.Zero(rv.Type())
return !reflect.DeepEqual(rv.Interface(), zv.Interface()), "should not be empty"
}
func StdEq(v interface{}, cmp string) (bool, string) {
eq, err := compare(v, cmp)
if err != nil {
return false, err.Error()
}
return eq == CompareEqual, fmt.Sprintf("should be equal to %s", cmp)
}
func StdNe(v interface{}, cmp string) (bool, string) {
eq, err := compare(v, cmp)
if err != nil {
return false, err.Error()
}
return eq != CompareEqual, fmt.Sprintf("should not be equal to %s", cmp)
}
func StdGt(v interface{}, cmp string) (bool, string) {
eq, err := compare(v, cmp)
if err != nil {
return false, err.Error()
}
return eq == CompareGreaterThan, fmt.Sprintf("should be greater than %s", cmp)
}
func StdGte(v interface{}, cmp string) (bool, string) {
eq, err := compare(v, cmp)
if err != nil {
return false, err.Error()
}
return (CompareEqual|CompareGreaterThan)&eq > 0, fmt.Sprintf("should be greater or equal to %s", cmp)
}
func StdLt(v interface{}, cmp string) (bool, string) {
eq, err := compare(v, cmp)
if err != nil {
return false, err.Error()
}
return eq == CompareLessThan, fmt.Sprintf("should be less than %s", cmp)
}
func StdLte(v interface{}, cmp string) (bool, string) {
eq, err := compare(v, cmp)
if err != nil {
return false, err.Error()
}
return (CompareEqual|CompareLessThan)&eq > 0, fmt.Sprintf("should be less or equal to %s", cmp)
}
func StdRange(v interface{}, low, high string) (bool, string) {
eq, err := compare(v, low)
if err != nil {
return false, err.Error()
}
if (CompareEqual|CompareGreaterThan)&eq > 0 {
eq, err = compare(v, high)
if err != nil {
return false, err.Error()
}
if (CompareEqual|CompareLessThan)&eq > 0 {
return true, ""
}
}
return false, fmt.Sprintf("should be in the range [%s, %s]", low, high)
}
func StdEnum(v interface{}, opts ...string) (bool, string) {
for _, opt := range opts {
eq, err := compare(v, opt)
if err != nil {
return false, err.Error()
}
if CompareEqual&eq > 0 {
return true, ""
}
}
return false, fmt.Sprintf("should be in range %+v", opts)
}
func StdLen(v interface{}, maxlen int) (bool, string) {
err := fmt.Sprintf("length must be exactly %d", maxlen)
if s, ok := v.(string); ok {
return len(s) == maxlen, err
} else if s, ok := v.(stringer); ok {
return len(s.String()) == maxlen, err
}
return false, fmt.Sprintf("unexpected string type: %t", v)
}
func StdMaxLen(v interface{}, maxlen int) (bool, string) {
err := fmt.Sprintf("length must be up to %d", maxlen)
if s, ok := v.(string); ok {
return len(s) <= maxlen, err
} else if s, ok := v.(stringer); ok {
return len(s.String()) <= maxlen, err
}
return false, fmt.Sprintf("unexpected string type: %t", v)
}