-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric.go
More file actions
327 lines (253 loc) · 8.19 KB
/
Copy pathnumeric.go
File metadata and controls
327 lines (253 loc) · 8.19 KB
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package validator
import (
"math"
"reflect"
"strconv"
"strings"
"github.com/libtnb/validator/conv"
)
func init() {
registerRules(
&minRule{},
&maxRule{},
&betweenRule{},
>Rule{},
>eRule{},
<Rule{},
<eRule{},
&lenRule{},
&sizeRule{},
&digitsRule{},
&numericRule{},
&numberRule{},
&booleanRule{},
)
}
var (
_ Rule = (*minRule)(nil)
_ Rule = (*maxRule)(nil)
_ Rule = (*betweenRule)(nil)
_ Rule = (*gtRule)(nil)
_ Rule = (*gteRule)(nil)
_ Rule = (*ltRule)(nil)
_ Rule = (*lteRule)(nil)
_ Rule = (*lenRule)(nil)
_ Rule = (*sizeRule)(nil)
_ Rule = (*digitsRule)(nil)
_ Rule = (*numericRule)(nil)
_ Rule = (*numberRule)(nil)
_ Rule = (*booleanRule)(nil)
_ leafCompiler = (*digitsRule)(nil)
)
// numericHintForm marks size rules whose string comparison switches from rune
// length to numeric value when the expression asserts numeric/number.
type numericHintForm interface{ withNumericHint() Rule }
type minRule struct{ numeric bool }
func (r *minRule) Signature() string { return "min" }
func (r *minRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c >= 0 })
}
func (r *minRule) Message() string { return "The {field} field must be at least {0}." }
func (r *minRule) withNumericHint() Rule { return &minRule{numeric: true} }
type maxRule struct{ numeric bool }
func (r *maxRule) Signature() string { return "max" }
func (r *maxRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c <= 0 })
}
func (r *maxRule) Message() string { return "The {field} field must not be greater than {0}." }
func (r *maxRule) withNumericHint() Rule { return &maxRule{numeric: true} }
type betweenRule struct{ numeric bool }
func (r *betweenRule) Signature() string { return "between" }
func (r *betweenRule) Passes(f *Field) bool {
rv := f.Reflect()
if isEmptyV(rv) {
return true
}
attrs := f.Attrs()
if len(attrs) < 2 {
return false
}
lo, ok := compareSize(rv, attrs[0], r.numeric)
if !ok || lo < 0 {
return false
}
hi, ok := compareSize(rv, attrs[1], r.numeric)
return ok && hi <= 0
}
func (r *betweenRule) Message() string { return "The {field} field must be between {0} and {1}." }
func (r *betweenRule) withNumericHint() Rule { return &betweenRule{numeric: true} }
type gtRule struct{ numeric bool }
func (r *gtRule) Signature() string { return "gt" }
func (r *gtRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c > 0 })
}
func (r *gtRule) Message() string { return "The {field} field must be greater than {0}." }
func (r *gtRule) withNumericHint() Rule { return >Rule{numeric: true} }
type gteRule struct{ numeric bool }
func (r *gteRule) Signature() string { return "gte" }
func (r *gteRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c >= 0 })
}
func (r *gteRule) Message() string { return "The {field} field must be greater than or equal to {0}." }
func (r *gteRule) withNumericHint() Rule { return >eRule{numeric: true} }
type ltRule struct{ numeric bool }
func (r *ltRule) Signature() string { return "lt" }
func (r *ltRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c < 0 })
}
func (r *ltRule) Message() string { return "The {field} field must be less than {0}." }
func (r *ltRule) withNumericHint() Rule { return <Rule{numeric: true} }
type lteRule struct{ numeric bool }
func (r *lteRule) Signature() string { return "lte" }
func (r *lteRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c <= 0 })
}
func (r *lteRule) Message() string { return "The {field} field must be less than or equal to {0}." }
func (r *lteRule) withNumericHint() Rule { return <eRule{numeric: true} }
type lenRule struct{ numeric bool }
func (r *lenRule) Signature() string { return "len" }
func (r *lenRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c == 0 })
}
func (r *lenRule) Message() string { return "The {field} field must be {0}." }
func (r *lenRule) withNumericHint() Rule { return &lenRule{numeric: true} }
type sizeRule struct{ numeric bool }
func (r *sizeRule) Signature() string { return "size" }
func (r *sizeRule) Passes(f *Field) bool {
return sizeCompare(f, r.numeric, func(c int) bool { return c == 0 })
}
func (r *sizeRule) Message() string { return "The {field} field must be {0}." }
func (r *sizeRule) withNumericHint() Rule { return &sizeRule{numeric: true} }
type digitsRule struct{}
func (r *digitsRule) Signature() string { return "digits" }
func (r *digitsRule) Passes(f *Field) bool {
attrs := f.Attrs()
if len(attrs) < 1 {
return isEmptyV(f.Reflect())
}
want, err := strconv.ParseInt(strings.TrimSpace(attrs[0]), 10, 64)
if err != nil {
return isEmptyV(f.Reflect())
}
return digitsCheck(f, want)
}
func (r *digitsRule) Message() string { return "The {field} field must be {0} digits." }
// compilePasses pre-parses the length so evaluation never re-parses the arg.
func (r *digitsRule) compilePasses(args []string) func(*Field) bool {
if len(args) < 1 {
return nil // fall back to the generic path (fails closed on non-empty)
}
want, err := strconv.ParseInt(strings.TrimSpace(args[0]), 10, 64)
if err != nil {
return nil
}
return func(f *Field) bool { return digitsCheck(f, want) }
}
type numericRule struct{}
func (r *numericRule) Signature() string { return "numeric" }
func (r *numericRule) Passes(f *Field) bool {
if isEmptyV(f.Reflect()) {
return true
}
_, ok := numericValue(f.Reflect())
return ok
}
func (r *numericRule) Message() string { return "The {field} field must be a number." }
type numberRule struct{}
func (r *numberRule) Signature() string { return "number" }
func (r *numberRule) Passes(f *Field) bool {
rv := f.Reflect()
if isEmptyV(rv) {
return true
}
switch rv.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return true
case reflect.Float32, reflect.Float64:
x := rv.Float()
return !math.IsInf(x, 0) && !math.IsNaN(x) && x == math.Trunc(x)
case reflect.String:
return intLikeString(rv.String())
case reflect.Slice:
if rv.Type().Elem().Kind() == reflect.Uint8 {
return intLikeString(string(rv.Bytes()))
}
return false
default:
return false
}
}
func (r *numberRule) Message() string { return "The {field} field must be an integer." }
type booleanRule struct{}
func (r *booleanRule) Signature() string { return "boolean" }
func (r *booleanRule) Passes(f *Field) bool {
rv := f.Reflect()
if isEmptyV(rv) {
return true
}
switch {
case rv.Kind() == reflect.Bool:
return true
case rv.Kind() == reflect.String:
return boolLikeString(rv.String())
case rv.Kind() == reflect.Slice && rv.Type().Elem().Kind() == reflect.Uint8:
return boolLikeString(string(rv.Bytes()))
default:
if n, ok := numericValue(rv); ok {
return n == 0 || n == 1
}
return false
}
}
func (r *booleanRule) Message() string { return "The {field} field must be true or false." }
func digitsCheck(f *Field, want int64) bool {
rv := f.Reflect()
if isEmptyV(rv) {
return true
}
s := valString(rv)
if int64(len(s)) != want {
return false
}
for _, c := range s {
if c < '0' || c > '9' {
return false
}
}
return true
}
// intLikeString reports whether s is an integer string (uint64-range positives count).
func intLikeString(s string) bool {
s = strings.TrimSpace(s)
if _, err := strconv.ParseInt(s, 10, 64); err == nil {
return true
}
_, err := strconv.ParseUint(s, 10, 64)
return err == nil
}
// boolLikeString rejects whitespace-only input: blank is not a boolean.
func boolLikeString(s string) bool {
if strings.TrimSpace(s) == "" {
return false
}
_, err := conv.ParseBool(s)
return err == nil
}
// sizeCompare: single-threshold size family over compareSize's three-way
// result; fails closed on a bad threshold or unmeasurable value.
func sizeCompare(f *Field, numericHint bool, ok func(c int) bool) bool {
rv := f.Reflect()
if isEmptyV(rv) {
return true
}
attrs := f.Attrs()
if len(attrs) < 1 {
return false
}
c, valid := compareSize(rv, attrs[0], numericHint)
if !valid {
return false
}
return ok(c)
}