forked from lauyoume/gopiper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
221 lines (199 loc) · 6.71 KB
/
validator.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
package gopiper
import (
"reflect"
"regexp"
"strconv"
"unicode/utf8"
"github.com/admpub/regexp2"
"github.com/webx-top/com"
)
func init() {
// 验证器类型的过滤器统一用下划线开头(验证不通过时,返回ErrInvalidContent错误便于后续处理)
RegisterFilter("_required", required, "非空", `_required`, ``)
RegisterFilter("_email", email, "E-mail地址", `_email`, ``)
RegisterFilter("_username", username, "用户名(字母/数字/汉字)", `_username`, ``)
RegisterFilter("_singleline", singleline, "单行文本", `_singleline`, ``)
RegisterFilter("_mutiline", mutiline, "多行文本", `_mutiline`, ``)
RegisterFilter("_url", url, "URL", `_url`, ``)
RegisterFilter("_chinese", chinese, "全是汉字", `_chinese`, ``)
RegisterFilter("_haschinese", haschinese, "包含汉字", `_haschinese`, ``)
RegisterFilter("_minsize", minsize, "最小长度", `_minsize(5)`, ``)
RegisterFilter("_maxsize", maxsize, "最大长度", `_maxsize(5)`, ``)
RegisterFilter("_size", size, "匹配长度", `_size(5)`, ``)
RegisterFilter("_alpha", alpha, "字母", `_alpha`, ``)
RegisterFilter("_alphanum", alphanum, "字母或数字", `_alphanum`, ``)
RegisterFilter("_numeric", numeric, "纯数字", `_numeric`, ``)
RegisterFilter("_match", match, "正则匹配", `_match([a-z]+)`, ``)
RegisterFilter("_unmatch", unmatch, "正则不匹配", `_unmatch([a-z]+)`, ``)
RegisterFilter("_match2", match2, "正则匹配(兼容Perl5和.NET)", `_match2([a-z]+)`, ``)
RegisterFilter("_unmatch2", unmatch2, "正则不匹配(兼容Perl5和.NET)", `_unmatch2([a-z]+)`, ``)
}
func required(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if len(v) == 0 {
return v, ErrInvalidContent
}
return v, nil
})
}
func email(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !com.IsEmailRFC(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func username(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !com.IsUsername(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func singleline(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !com.IsSingleLineText(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func mutiline(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !com.IsMultiLineText(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func url(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !com.IsURL(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func chinese(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !com.IsChinese(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func haschinese(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !com.HasChinese(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func minsize(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
minSize, _ := strconv.Atoi(params.String())
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if utf8.RuneCountInString(v) < minSize {
return v, ErrInvalidContent
}
return v, nil
})
}
func maxsize(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
maxSize, _ := strconv.Atoi(params.String())
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if utf8.RuneCountInString(v) > maxSize {
return v, ErrInvalidContent
}
return v, nil
})
}
func size(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
size, _ := strconv.Atoi(params.String())
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if utf8.RuneCountInString(v) != size {
return v, ErrInvalidContent
}
return v, nil
})
}
func alpha(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
for _, v := range v {
if !com.IsAlpha(v) {
return v, ErrInvalidContent
}
}
return v, nil
})
}
func alphanum(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
for _, v := range v {
if !com.IsAlphaNumeric(v) {
return v, ErrInvalidContent
}
}
return v, nil
})
}
func numeric(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
for _, v := range v {
if !com.IsNumeric(v) {
return v, ErrInvalidContent
}
}
return v, nil
})
}
func match(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
re, err := regexp.Compile(params.String())
if err != nil {
return src.Interface(), err
}
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if !re.MatchString(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func unmatch(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
re, err := regexp.Compile(params.String())
if err != nil {
return src.Interface(), err
}
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if re.MatchString(v) {
return v, ErrInvalidContent
}
return v, nil
})
}
func match2(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
re, err := regexp2.Compile(params.String(), 0)
if err != nil {
return src.Interface(), err
}
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if ok, _ := re.MatchString(v); !ok {
return v, ErrInvalidContent
}
return v, nil
})
}
func unmatch2(pipe *PipeItem, src *reflect.Value, params *reflect.Value) (interface{}, error) {
re, err := regexp2.Compile(params.String(), 0)
if err != nil {
return src.Interface(), err
}
return _filterValue(src.Interface(), func(v string) (interface{}, error) {
if ok, _ := re.MatchString(v); ok {
return v, ErrInvalidContent
}
return v, nil
})
}