forked from go-validator/validator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltins.go
304 lines (289 loc) · 8.81 KB
/
builtins.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
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
// Package validator implements value validations
//
// Copyright 2014 Roberto Teixeira <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package walidator
import (
"errors"
"reflect"
"regexp"
"strconv"
)
const (
errZeroValue = "zero value"
errMin = "less than min"
errMax = "greater than max"
errLen = "invalid length"
errRegexp = "regular expression mismatch"
errRequired = "required value"
)
var errUnsupportedType = errors.New("unsupported type")
var errBadParameter = errors.New("bad parameter")
// nonzero tests whether a variable value non-zero
// as defined by the golang spec.
func nonzero(t reflect.Type, param string) (ValidationFunc, error) {
check := func(ok bool, r *ErrorReporter) {
if !ok {
r.Errorf(errZeroValue)
}
}
switch t.Kind() {
case reflect.String:
return func(v reflect.Value, r *ErrorReporter) {
check(len(v.String()) != 0, r)
}, nil
case reflect.Ptr, reflect.Interface:
return func(v reflect.Value, r *ErrorReporter) {
check(!v.IsNil(), r)
}, nil
case reflect.Slice, reflect.Map, reflect.Array:
return func(v reflect.Value, r *ErrorReporter) {
check(v.Len() != 0, r)
}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return func(v reflect.Value, r *ErrorReporter) {
check(v.Int() != 0, r)
}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return func(v reflect.Value, r *ErrorReporter) {
check(v.Uint() != 0, r)
}, nil
case reflect.Float32, reflect.Float64:
return func(v reflect.Value, r *ErrorReporter) {
// TODO this preserves old behavior but is arguably
// incorrect - it should probably error if the value is NaN.
check(v.Float() != 0, r)
}, nil
case reflect.Bool:
return func(v reflect.Value, r *ErrorReporter) {
check(v.Bool(), r)
}, nil
case reflect.Struct:
return okValidation, nil
}
return nil, errUnsupportedType
}
// length tests whether a variable's length is equal to a given
// value. For strings it tests the number of characters whereas
// for maps and slices it tests the number of items.
func length(t reflect.Type, param string) (ValidationFunc, error) {
switch t.Kind() {
case reflect.String, reflect.Slice, reflect.Map, reflect.Array:
p, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if int64(v.Len()) != p {
r.Errorf(errLen)
}
}, nil
}
return nil, errUnsupportedType
}
// min tests whether a variable value is larger or equal to a given
// number. For number types, it's a simple lesser-than test; for
// strings it tests the number of characters whereas for maps
// and slices it tests the number of items.
func min(t reflect.Type, param string) (ValidationFunc, error) {
switch t.Kind() {
case reflect.String, reflect.Slice, reflect.Map, reflect.Array:
p, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if int64(v.Len()) < p {
r.Errorf(errMin)
}
}, nil
case reflect.Float32, reflect.Float64:
p, err := strconv.ParseFloat(param, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if v.Float() < p {
r.Errorf(errMin)
}
}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if v.Int() < p {
r.Errorf(errMin)
}
}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
p, err := strconv.ParseUint(param, 0, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if v.Uint() < p {
r.Errorf(errMin)
}
}, nil
default:
return nil, errUnsupportedType
}
}
// max tests whether a variable value is lesser than a given
// value. For numbers, it's a simple lesser-than test; for
// strings it tests the number of characters whereas for maps
// and slices it tests the number of items.
func max(t reflect.Type, param string) (ValidationFunc, error) {
switch t.Kind() {
case reflect.String, reflect.Slice, reflect.Map, reflect.Array:
p, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if int64(v.Len()) > p {
r.Errorf(errMax)
}
}, nil
case reflect.Float32, reflect.Float64:
p, err := strconv.ParseFloat(param, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if v.Float() > p {
r.Errorf(errMax)
}
}, nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
p, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if v.Int() > p {
r.Errorf(errMax)
}
}, nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
p, err := strconv.ParseUint(param, 0, 64)
if err != nil {
return nil, errBadParameter
}
return func(v reflect.Value, r *ErrorReporter) {
if v.Uint() > p {
r.Errorf(errMax)
}
}, nil
default:
return nil, errUnsupportedType
}
}
// regex is the builtin validation function that checks
// whether the string variable matches a regular expression
func regex(t reflect.Type, param string) (ValidationFunc, error) {
re, err := regexp.Compile(param)
if err != nil {
return nil, errBadParameter
}
if t != reflect.TypeOf("") {
return nil, errUnsupportedType
}
return func(v reflect.Value, r *ErrorReporter) {
if !re.MatchString(v.String()) {
r.Errorf(errRegexp)
}
}, nil
}
// required validates the value is not nil for a field, that is, a
// pointer or an interface, any other case is a valid one as zero
// value from Go spec.
func required(t reflect.Type, param string) (ValidationFunc, error) {
switch t.Kind() {
case reflect.Ptr, reflect.Interface, reflect.Map, reflect.Slice:
return func(v reflect.Value, r *ErrorReporter) {
if v.IsNil() {
r.Errorf(errRequired)
}
}, nil
case reflect.String, reflect.Array, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr, reflect.Float32, reflect.Float64, reflect.Bool, reflect.Struct:
return okValidation, nil
default:
return nil, errUnsupportedType
}
}
var uuidRE = regexp.MustCompile(`(?i)^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`)
// uuid validates if a string represents a valid UUID (RFC 4122)
func uuid(t reflect.Type, param string) (ValidationFunc, error) {
if t.Kind() != reflect.String {
return nil, errUnsupportedType
}
return func(v reflect.Value, r *ErrorReporter) {
if !uuidRE.MatchString(v.String()) {
r.Errorf(errRegexp)
}
}, nil
}
// latitude validates that a field is a latitude
func latitude(t reflect.Type, param string) (ValidationFunc, error) {
validateLatitude := func(f float64, r *ErrorReporter) {
if f < -90 || f > 90 {
r.Errorf("%g is not a valid latitude", f)
}
}
switch t.Kind() {
case reflect.Float64:
return func(v reflect.Value, r *ErrorReporter) {
validateLatitude(v.Float(), r)
}, nil
case reflect.String:
return func(v reflect.Value, r *ErrorReporter) {
s := v.String()
f, err := strconv.ParseFloat(s, 64)
if err != nil {
r.Errorf("%q is not a valid latitude", s)
}
validateLatitude(f, r)
}, nil
default:
return nil, errUnsupportedType
}
}
// longitude validates that a field is a longitude
func longitude(t reflect.Type, param string) (ValidationFunc, error) {
validateLongitude := func(f float64, r *ErrorReporter) {
if f < -180 || f > 180 {
r.Errorf("%g is not a valid longitude", f)
}
}
switch t.Kind() {
case reflect.Float64:
return func(v reflect.Value, r *ErrorReporter) {
validateLongitude(v.Float(), r)
}, nil
case reflect.String:
return func(v reflect.Value, r *ErrorReporter) {
s := v.String()
f, err := strconv.ParseFloat(s, 64)
if err != nil {
r.Errorf("%q is not a valid longitude", s)
}
validateLongitude(f, r)
}, nil
default:
return nil, errUnsupportedType
}
}