-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilt_in.go
More file actions
282 lines (231 loc) · 6.3 KB
/
built_in.go
File metadata and controls
282 lines (231 loc) · 6.3 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
package firevault
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"unicode/utf8"
)
const restrictedTagChars = ".[],|=+()`~!@#$%^&*\\\"/?<>{}"
var (
restrictedRules = map[string]struct{}{
"dive": {},
"omitempty": {},
"omitempty_create": {},
"omitempty_update": {},
"omitempty_validate": {},
}
builtInValidators = map[string]ValidationFunc{
"required": validateRequired,
"required_create": validateRequired,
"required_update": validateRequired,
"required_validate": validateRequired,
"email": validateEmail,
"max": validateMax,
"min": validateMin,
}
builtInTransformators = map[string]TransformationFunc{
"uppercase": transformUppercase,
"lowercase": transformLowercase,
"trim_space": transformTrimSpace,
}
)
// validates if field is of supported type
func isSupported(kind reflect.Kind) bool {
if kind == reflect.Invalid || kind == reflect.Chan || kind == reflect.Func {
return false
}
return true
}
// validates if field's value is the default static value
func isZero(kind reflect.Kind, value reflect.Value) bool {
if kind == reflect.Slice || kind == reflect.Map || kind == reflect.Pointer || kind == reflect.Interface {
return value.IsNil()
}
return !value.IsValid() || value.IsZero()
}
// validates if field is zero
func validateRequired(fs FieldScope) (bool, error) {
return !isZero(fs.Kind(), fs.Value()), nil
}
// validates if field is a valid email address
func validateEmail(fs FieldScope) (bool, error) {
return emailRegex().MatchString(fs.Value().String()), nil
}
// validates if field's value is less than or equal to param's value
func validateMax(fs FieldScope) (bool, error) {
return checkBoundary(fs, true)
}
// validates if field's value is greater than or equal to param's value
func validateMin(fs FieldScope) (bool, error) {
return checkBoundary(fs, false)
}
// performs boundary validation - isMax: true for maximum boundary,
// false for minimum boundary
func checkBoundary(fs FieldScope, isMax bool) (bool, error) {
param := fs.Param()
if param == "" {
boundaryType := "max"
if !isMax {
boundaryType = "min"
}
return false, fmt.Errorf(
"firevault: '%s' validation requires a parameter for field '%s'",
boundaryType,
fs.Path(),
)
}
kind := fs.Kind()
val := fs.Value()
// check string length (characters)
if kind == reflect.String {
threshold, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return false, fmt.Errorf(
"firevault: failed to parse '%s' parameter for field '%s': %w",
param,
fs.Path(),
err,
)
}
length := int64(utf8.RuneCountInString(val.String()))
if isMax {
return length <= threshold, nil
}
return length >= threshold, nil
}
// check length types
if kind == reflect.Slice || kind == reflect.Array || kind == reflect.Map {
threshold, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return false, fmt.Errorf(
"firevault: failed to parse '%s' parameter for field '%s': %w",
param,
fs.Path(),
err,
)
}
length := int64(val.Len())
if isMax {
return length <= threshold, nil
}
return length >= threshold, nil
}
// check signed ints
if kind == reflect.Int || kind == reflect.Int8 || kind == reflect.Int16 || kind == reflect.Int32 ||
kind == reflect.Int64 {
threshold, err := strconv.ParseInt(param, 0, 64)
if err != nil {
return false, fmt.Errorf(
"firevault: failed to parse '%s' parameter for field '%s': %w",
param,
fs.Path(),
err,
)
}
value := val.Int()
if isMax {
return value <= threshold, nil
}
return value >= threshold, nil
}
// check unsigned ints
if kind == reflect.Uint || kind == reflect.Uint8 || kind == reflect.Uint16 || kind == reflect.Uint32 ||
kind == reflect.Uint64 {
threshold, err := strconv.ParseUint(param, 0, 64)
if err != nil {
return false, fmt.Errorf(
"firevault: failed to parse '%s' parameter for field '%s': %w",
param,
fs.Path(),
err,
)
}
value := val.Uint()
if isMax {
return value <= threshold, nil
}
return value >= threshold, nil
}
// check floats
if kind == reflect.Float32 || kind == reflect.Float64 {
threshold, err := strconv.ParseFloat(param, 32)
if err != nil {
return false, fmt.Errorf(
"firevault: failed to parse '%s' parameter for field '%s': %w",
param,
fs.Path(),
err,
)
}
value := val.Float()
if isMax {
return value <= threshold, nil
}
return value >= threshold, nil
}
// check times
if kind == reflect.Struct {
timeType := reflect.TypeOf(time.Time{})
if fs.Type().ConvertibleTo(timeType) {
params := strings.Split(param, "|")
if len(params) < 2 {
return false, fmt.Errorf(
"firevault: time validation for field '%s' requires format 'layout|value', got: %s",
fs.Path(),
param,
)
}
threshold, err := time.Parse(params[0], params[1])
if err != nil {
return false, fmt.Errorf(
"firevault: failed to parse time for field '%s' with layout '%s' and value '%s': %w",
fs.Path(),
params[0],
params[1],
err,
)
}
t, ok := fs.Value().Convert(timeType).Interface().(time.Time)
if !ok {
return false, fmt.Errorf(
"firevault: failed parse time for field '%s': unexpected time type: %T",
fs.Path(),
timeType,
)
}
if isMax {
return (t.Before(threshold) || t.Equal(threshold)), nil
}
return (t.After(threshold) || t.Equal(threshold)), nil
}
}
return false, fmt.Errorf(
"firevault: '%s' validation not supported for field type %s at '%s'",
fs.Rule(),
fs.Kind(),
fs.Path(),
)
}
// transforms a field of string type to upper case
func transformUppercase(fs FieldScope) (interface{}, error) {
if fs.Kind() != reflect.String {
return fs.Value().Interface(), nil
}
return strings.ToUpper(fs.Value().String()), nil
}
// transforms a field of string type to lower case
func transformLowercase(fs FieldScope) (interface{}, error) {
if fs.Kind() != reflect.String {
return fs.Value().Interface(), nil
}
return strings.ToLower(fs.Value().String()), nil
}
// transforms a field of string type by removing all white space
func transformTrimSpace(fs FieldScope) (interface{}, error) {
if fs.Kind() != reflect.String {
return fs.Value().Interface(), nil
}
return strings.TrimSpace(fs.Value().String()), nil
}