-
Notifications
You must be signed in to change notification settings - Fork 223
/
minmax.go
195 lines (173 loc) · 5.58 KB
/
minmax.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Copyright 2016 Qiang Xue. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package validation
import (
"fmt"
"reflect"
"time"
)
var (
// ErrMinGreaterEqualThanRequired is the error that returns when a value is less than a specified threshold.
ErrMinGreaterEqualThanRequired = NewError("validation_min_greater_equal_than_required", "must be no less than {{.threshold}}")
// ErrMaxLessEqualThanRequired is the error that returns when a value is greater than a specified threshold.
ErrMaxLessEqualThanRequired = NewError("validation_max_less_equal_than_required", "must be no greater than {{.threshold}}")
// ErrMinGreaterThanRequired is the error that returns when a value is less than or equal to a specified threshold.
ErrMinGreaterThanRequired = NewError("validation_min_greater_than_required", "must be greater than {{.threshold}}")
// ErrMaxLessThanRequired is the error that returns when a value is greater than or equal to a specified threshold.
ErrMaxLessThanRequired = NewError("validation_max_less_than_required", "must be less than {{.threshold}}")
)
// ThresholdRule is a validation rule that checks if a value satisfies the specified threshold requirement.
type ThresholdRule struct {
threshold interface{}
operator int
err Error
}
const (
greaterThan = iota
greaterEqualThan
lessThan
lessEqualThan
)
// Min returns a validation rule that checks if a value is greater or equal than the specified value.
// By calling Exclusive, the rule will check if the value is strictly greater than the specified value.
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Min(min interface{}) ThresholdRule {
return ThresholdRule{
threshold: min,
operator: greaterEqualThan,
err: ErrMinGreaterEqualThanRequired,
}
}
// Max returns a validation rule that checks if a value is less or equal than the specified value.
// By calling Exclusive, the rule will check if the value is strictly less than the specified value.
// Note that the value being checked and the threshold value must be of the same type.
// Only int, uint, float and time.Time types are supported.
// An empty value is considered valid. Please use the Required rule to make sure a value is not empty.
func Max(max interface{}) ThresholdRule {
return ThresholdRule{
threshold: max,
operator: lessEqualThan,
err: ErrMaxLessEqualThanRequired,
}
}
// Exclusive sets the comparison to exclude the boundary value.
func (r ThresholdRule) Exclusive() ThresholdRule {
if r.operator == greaterEqualThan {
r.operator = greaterThan
r.err = ErrMinGreaterThanRequired
} else if r.operator == lessEqualThan {
r.operator = lessThan
r.err = ErrMaxLessThanRequired
}
return r
}
// Validate checks if the given value is valid or not.
func (r ThresholdRule) Validate(value interface{}) error {
value, isNil := Indirect(value)
if isNil || IsEmpty(value) {
return nil
}
rv := reflect.ValueOf(r.threshold)
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
v, err := ToInt(value)
if err != nil {
return err
}
if r.compareInt(rv.Int(), v) {
return nil
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
v, err := ToUint(value)
if err != nil {
return err
}
if r.compareUint(rv.Uint(), v) {
return nil
}
case reflect.Float32, reflect.Float64:
v, err := ToFloat(value)
if err != nil {
return err
}
if r.compareFloat(rv.Float(), v) {
return nil
}
case reflect.Struct:
t, ok := r.threshold.(time.Time)
if !ok {
return fmt.Errorf("type not supported: %v", rv.Type())
}
v, ok := value.(time.Time)
if !ok {
return fmt.Errorf("cannot convert %v to time.Time", reflect.TypeOf(value))
}
if v.IsZero() || r.compareTime(t, v) {
return nil
}
default:
return fmt.Errorf("type not supported: %v", rv.Type())
}
return r.err.SetParams(map[string]interface{}{"threshold": r.threshold})
}
// Error sets the error message for the rule.
func (r ThresholdRule) Error(message string) ThresholdRule {
r.err = r.err.SetMessage(message)
return r
}
// ErrorObject sets the error struct for the rule.
func (r ThresholdRule) ErrorObject(err Error) ThresholdRule {
r.err = err
return r
}
func (r ThresholdRule) compareInt(threshold, value int64) bool {
switch r.operator {
case greaterThan:
return value > threshold
case greaterEqualThan:
return value >= threshold
case lessThan:
return value < threshold
default:
return value <= threshold
}
}
func (r ThresholdRule) compareUint(threshold, value uint64) bool {
switch r.operator {
case greaterThan:
return value > threshold
case greaterEqualThan:
return value >= threshold
case lessThan:
return value < threshold
default:
return value <= threshold
}
}
func (r ThresholdRule) compareFloat(threshold, value float64) bool {
switch r.operator {
case greaterThan:
return value > threshold
case greaterEqualThan:
return value >= threshold
case lessThan:
return value < threshold
default:
return value <= threshold
}
}
func (r ThresholdRule) compareTime(threshold, value time.Time) bool {
switch r.operator {
case greaterThan:
return value.After(threshold)
case greaterEqualThan:
return value.After(threshold) || value.Equal(threshold)
case lessThan:
return value.Before(threshold)
default:
return value.Before(threshold) || value.Equal(threshold)
}
}