-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.go
More file actions
358 lines (321 loc) · 9.13 KB
/
Copy pathrules.go
File metadata and controls
358 lines (321 loc) · 9.13 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package govalidate
import (
"fmt"
"net"
"net/url"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"unicode"
)
// RuleFunc validates a field value and returns an error message if invalid.
// value is the field value, param is the rule parameter (e.g., "10" from "min=10").
type RuleFunc func(value reflect.Value, param string) string
// builtinRules contains all built-in validation rules.
var builtinRules = map[string]RuleFunc{
"required": ruleRequired,
"min": ruleMin,
"max": ruleMax,
"len": ruleLen,
"email": ruleEmail,
"url": ruleURL,
"ip": ruleIP,
"alpha": ruleAlpha,
"alphanum": ruleAlphaNum,
"numeric": ruleNumeric,
"lowercase": ruleLowercase,
"uppercase": ruleUppercase,
"contains": ruleContains,
"startswith": ruleStartsWith,
"endswith": ruleEndsWith,
"oneof": ruleOneOf,
"regex": ruleRegex,
"datetime": ruleDatetime,
"gt": ruleGT,
"gte": ruleGTE,
"lt": ruleLT,
"lte": ruleLTE,
}
// ─── Required ─────────────────────────────────────────────────────
func ruleRequired(v reflect.Value, _ string) string {
if isZero(v) {
return "is required"
}
return ""
}
func isZero(v reflect.Value) bool {
switch v.Kind() {
case reflect.String:
return strings.TrimSpace(v.String()) == ""
case reflect.Bool:
return false // bool is always "present"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Slice, reflect.Map, reflect.Array:
return v.Len() == 0
case reflect.Ptr, reflect.Interface:
return v.IsNil()
default:
return v.IsZero()
}
}
// ─── Length / Size ────────────────────────────────────────────────
func ruleMin(v reflect.Value, param string) string {
n, err := strconv.ParseFloat(param, 64)
if err != nil {
return fmt.Sprintf("invalid min parameter: %s", param)
}
switch v.Kind() {
case reflect.String:
if float64(len(v.String())) < n {
return fmt.Sprintf("must be at least %s characters", param)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if float64(v.Int()) < n {
return fmt.Sprintf("must be at least %s", param)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if float64(v.Uint()) < n {
return fmt.Sprintf("must be at least %s", param)
}
case reflect.Float32, reflect.Float64:
if v.Float() < n {
return fmt.Sprintf("must be at least %s", param)
}
case reflect.Slice, reflect.Map, reflect.Array:
if float64(v.Len()) < n {
return fmt.Sprintf("must have at least %s items", param)
}
}
return ""
}
func ruleMax(v reflect.Value, param string) string {
n, err := strconv.ParseFloat(param, 64)
if err != nil {
return fmt.Sprintf("invalid max parameter: %s", param)
}
switch v.Kind() {
case reflect.String:
if float64(len(v.String())) > n {
return fmt.Sprintf("must be at most %s characters", param)
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if float64(v.Int()) > n {
return fmt.Sprintf("must be at most %s", param)
}
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if float64(v.Uint()) > n {
return fmt.Sprintf("must be at most %s", param)
}
case reflect.Float32, reflect.Float64:
if v.Float() > n {
return fmt.Sprintf("must be at most %s", param)
}
case reflect.Slice, reflect.Map, reflect.Array:
if float64(v.Len()) > n {
return fmt.Sprintf("must have at most %s items", param)
}
}
return ""
}
func ruleLen(v reflect.Value, param string) string {
n, _ := strconv.Atoi(param)
switch v.Kind() {
case reflect.String:
if len(v.String()) != n {
return fmt.Sprintf("must be exactly %s characters", param)
}
case reflect.Slice, reflect.Map, reflect.Array:
if v.Len() != n {
return fmt.Sprintf("must have exactly %s items", param)
}
}
return ""
}
// ─── Comparison ───────────────────────────────────────────────────
func ruleGT(v reflect.Value, param string) string {
return compareNumeric(v, param, ">", func(a, b float64) bool { return a > b })
}
func ruleGTE(v reflect.Value, param string) string {
return compareNumeric(v, param, ">=", func(a, b float64) bool { return a >= b })
}
func ruleLT(v reflect.Value, param string) string {
return compareNumeric(v, param, "<", func(a, b float64) bool { return a < b })
}
func ruleLTE(v reflect.Value, param string) string {
return compareNumeric(v, param, "<=", func(a, b float64) bool { return a <= b })
}
func compareNumeric(v reflect.Value, param, op string, cmp func(a, b float64) bool) string {
n, err := strconv.ParseFloat(param, 64)
if err != nil {
return fmt.Sprintf("invalid parameter: %s", param)
}
var val float64
switch v.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
val = float64(v.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val = float64(v.Uint())
case reflect.Float32, reflect.Float64:
val = v.Float()
default:
return ""
}
if !cmp(val, n) {
return fmt.Sprintf("must be %s %s", op, param)
}
return ""
}
// ─── Format Validators ───────────────────────────────────────────
var emailRegex = regexp.MustCompile(`^[a-zA-Z0-9._%+\-]+@[a-zA-Z0-9.\-]+\.[a-zA-Z]{2,}$`)
func ruleEmail(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
if !emailRegex.MatchString(v.String()) {
return "must be a valid email address"
}
return ""
}
func ruleURL(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
u, err := url.ParseRequestURI(v.String())
if err != nil || u.Scheme == "" || u.Host == "" {
return "must be a valid URL"
}
return ""
}
func ruleIP(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
if net.ParseIP(v.String()) == nil {
return "must be a valid IP address"
}
return ""
}
// ─── String Validators ───────────────────────────────────────────
func ruleAlpha(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
for _, r := range v.String() {
if !unicode.IsLetter(r) {
return "must contain only letters"
}
}
return ""
}
func ruleAlphaNum(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
for _, r := range v.String() {
if !unicode.IsLetter(r) && !unicode.IsDigit(r) {
return "must contain only letters and numbers"
}
}
return ""
}
func ruleNumeric(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
for _, r := range v.String() {
if !unicode.IsDigit(r) && r != '.' && r != '-' {
return "must be numeric"
}
}
return ""
}
func ruleLowercase(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
if v.String() != strings.ToLower(v.String()) {
return "must be lowercase"
}
return ""
}
func ruleUppercase(v reflect.Value, _ string) string {
if v.Kind() != reflect.String {
return ""
}
if v.String() != strings.ToUpper(v.String()) {
return "must be uppercase"
}
return ""
}
func ruleContains(v reflect.Value, param string) string {
if v.Kind() != reflect.String {
return ""
}
if !strings.Contains(v.String(), param) {
return fmt.Sprintf("must contain '%s'", param)
}
return ""
}
func ruleStartsWith(v reflect.Value, param string) string {
if v.Kind() != reflect.String {
return ""
}
if !strings.HasPrefix(v.String(), param) {
return fmt.Sprintf("must start with '%s'", param)
}
return ""
}
func ruleEndsWith(v reflect.Value, param string) string {
if v.Kind() != reflect.String {
return ""
}
if !strings.HasSuffix(v.String(), param) {
return fmt.Sprintf("must end with '%s'", param)
}
return ""
}
func ruleOneOf(v reflect.Value, param string) string {
if v.Kind() != reflect.String {
return ""
}
options := strings.Split(param, " ")
for _, opt := range options {
if v.String() == opt {
return ""
}
}
return fmt.Sprintf("must be one of: %s", strings.Join(options, ", "))
}
func ruleRegex(v reflect.Value, param string) string {
if v.Kind() != reflect.String {
return ""
}
re, err := regexp.Compile(param)
if err != nil {
return fmt.Sprintf("invalid regex pattern: %s", param)
}
if !re.MatchString(v.String()) {
return fmt.Sprintf("must match pattern: %s", param)
}
return ""
}
func ruleDatetime(v reflect.Value, param string) string {
if v.Kind() != reflect.String {
return ""
}
layout := param
if layout == "" {
layout = time.RFC3339
}
if _, err := time.Parse(layout, v.String()); err != nil {
return fmt.Sprintf("must be a valid datetime (%s)", layout)
}
return ""
}