-
Notifications
You must be signed in to change notification settings - Fork 4
/
rules.go
321 lines (287 loc) · 9.34 KB
/
rules.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
package galidator
import (
"context"
"net/mail"
"reflect"
"github.com/dlclark/regexp2"
"github.com/golodash/godash/generals"
"github.com/nyaruka/phonenumbers"
)
// A map which with rule or require's key will provide the default error message of that rule or require's key
var defaultValidatorErrorMessages = map[string]string{
// Rules
"int": "not an integer value",
"float": "not a float value",
"min": "$field's length must be higher equal to $min",
"max": "$field's length must be lower equal to $max",
"len_range": "$field's length must be between $from to $to characters long",
"len": "$field's length must be equal to $length",
"required": "required",
"non_zero": "can not be 0",
"non_nil": "can not be nil",
"non_empty": "can not be empty",
"email": "not a valid email address",
"regex": "$value does not pass /$pattern/ pattern",
"phone": "$value is not a valid international phone number format",
"map": "not a map",
"struct": "not a struct",
"slice": "not a slice",
"password": "$field must be at least 8 characters long and contain one lowercase, one uppercase, one special and one number character",
"or": "ruleSets in $field did not pass based on or logic",
"xor": "ruleSets in $field did not pass based on xor logic",
"choices": "$value does not include in allowed choices: $choices",
"string": "not a string",
"type": "not a $type",
// Requires
"when_exist_one": "$field is required because at least one of $choices fields are not nil, empty or zero(0, \"\", '')",
"when_exist_all": "$field is required because all of $choices fields are not nil, empty or zero(0, \"\", '')",
"when_not_exist_one": "$field is required because at least one of $choices fields are nil, empty or zero(0, \"\", '')",
"when_not_exist_all": "$field is required because all of $choices fields are nil, empty or zero(0, \"\", '')",
}
func isValid(input interface{}) bool {
return reflect.ValueOf(input).IsValid()
}
// Returns true if input is int
func intRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
inputValue := reflect.ValueOf(input)
switch inputValue.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64:
return true
default:
return false
}
}
// Returns true if input is float
func floatRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
inputValue := reflect.ValueOf(input)
switch inputValue.Kind() {
case reflect.Float32, reflect.Float64:
return true
default:
return false
}
}
// Returns true if input type is equal to defined type
func typeRule(t string) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
return isValid(input) && reflect.TypeOf(input).String() == t
}
}
// Returns true if: input >= min or len(input) >= min
func minRule(min float64) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
inputValue := reflect.ValueOf(input)
switch inputValue.Kind() {
case reflect.String, reflect.Map, reflect.Slice:
return inputValue.Len() >= int(min)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
return inputValue.Convert(reflect.TypeOf(1.0)).Float() >= min
default:
return false
}
}
}
// Returns true if: input <= max or len(input) <= max
func maxRule(max float64) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
inputValue := reflect.ValueOf(input)
switch inputValue.Kind() {
case reflect.String, reflect.Map, reflect.Slice:
return inputValue.Len() <= int(max)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
return inputValue.Convert(reflect.TypeOf(1.0)).Float() <= max
default:
return false
}
}
}
// Returns true if len(input) >= from && len(input) <= to
//
// If from == -1, no check on from config will happen
// If to == -1, no check on to config will happen
func lenRangeRule(from, to int) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
inputValue := reflect.ValueOf(input)
switch inputValue.Kind() {
case reflect.String, reflect.Map, reflect.Slice:
if from != -1 && inputValue.Len() < from {
return false
} else if to != -1 && inputValue.Len() > to {
return false
}
return true
default:
return false
}
}
}
// Returns true if len(input) is equal to passed length
func lenRule(length int) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
inputValue := reflect.ValueOf(input)
switch inputValue.Kind() {
case reflect.String, reflect.Map, reflect.Slice:
return inputValue.Len() == length
default:
return false
}
}
}
// Returns true if input is not 0, "", ”, nil and empty
func requiredRule(ctx context.Context, input interface{}) bool {
return !isNil(input) && !reflect.ValueOf(input).IsZero() && !hasZeroItems(input)
}
// Returns true if input is not zero(0, "", ”)
func nonZeroRule(ctx context.Context, input interface{}) bool {
return !isValid(input) || !reflect.ValueOf(input).IsZero()
}
// Returns true if input is not nil
func nonNilRule(ctx context.Context, input interface{}) bool {
return !isNil(input)
}
// Returns true if input has items
func nonEmptyRule(ctx context.Context, input interface{}) bool {
return !hasZeroItems(input)
}
// Returns true if input is a valid email
func emailRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
switch reflect.ValueOf(input).Kind() {
case reflect.String:
_, err := mail.ParseAddress(input.(string))
if err != nil {
return false
}
return true
default:
return false
}
}
// Returns true if input is a valid phone number
func phoneRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
InternationalPhoneRegex := regexp2.MustCompile(`^\+\d+$`, regexp2.None)
if ok, err := InternationalPhoneRegex.MatchString(input.(string)); !ok || err != nil {
return false
}
parsedNumber, err := phonenumbers.Parse(input.(string), "")
return err == nil && phonenumbers.IsValidNumber(parsedNumber)
}
// Returns true if input matches the passed pattern
func regexRule(pattern string) func(context.Context, interface{}) bool {
regex := regexp2.MustCompile(pattern, regexp2.None)
return func(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
inputValue := reflect.ValueOf(input)
switch inputValue.Kind() {
case reflect.String:
output, _ := regex.MatchString(input.(string))
return output
default:
return false
}
}
}
// Returns true if input is a map
func mapRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
return reflect.TypeOf(input).Kind() == reflect.Map
}
// Returns true if input is a struct
func structRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
return reflect.TypeOf(input).Kind() == reflect.Struct
}
// Returns true if input is a slice
func sliceRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
return reflect.TypeOf(input).Kind() == reflect.Slice
}
// Returns true if input is at least 8 characters long, has one lowercase, one uppercase, one special and one number character
func passwordRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
return regexRule(`^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$`)(ctx, input)
}
// If at least one of the passed ruleSets pass, this rule will pass
func orRule(ruleSets ...ruleSet) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
output := false
if len(ruleSets) == 0 {
return true
}
for _, ruleSet := range ruleSets {
output = len(ruleSet.validate(ctx, input)) == 0 || output
}
return output
}
}
// If Xor of the passed ruleSets pass, this rule will pass
func xorRule(ruleSets ...ruleSet) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
output := false
if len(ruleSets) == 0 {
return true
}
for _, ruleSet := range ruleSets {
output = len(ruleSet.validate(ctx, input)) == 0 != output
}
return output
}
}
// If passed data is not one of choices in choices variable, it fails
func choicesRule(choices ...interface{}) func(context.Context, interface{}) bool {
return func(ctx context.Context, input interface{}) bool {
for i := 0; i < len(choices); i++ {
element := choices[i]
if generals.Same(element, input) {
return true
}
}
return false
}
}
// Returns true if input is string
func stringRule(ctx context.Context, input interface{}) bool {
if !isValid(input) {
return false
}
return reflect.TypeOf(input).Kind() == reflect.String
}