-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules-list.go
More file actions
273 lines (204 loc) · 5.65 KB
/
Copy pathrules-list.go
File metadata and controls
273 lines (204 loc) · 5.65 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
package formvalidator
import (
"encoding/csv"
"errors"
"strings"
)
type inListSingle struct {
list []string
}
func InListSingle(list []string) Rule {
if list == nil {
list = []string{}
}
return &inListSingle{list}
}
/*
This is used in radio buttons and drop-downs (single entry)
fields []string - only one item is permitted, but with argument being a slice, it allows testing for multiple values
list []string - set by the programmer so it must be a slice
return error - FormError with message and extra data if revelant
*/
func (i *inListSingle) Validate(fields []string, errorMessages map[string]string) (error, []interface{}) {
if len(fields) == 0 || (len(fields) == 1 && len(fields[0]) == 0) { // if blank, it is fine, use required_single
return nil, nil
}
if len(fields) > 1 { // more than one entry, return error
return errors.New(errorMessages["multiple_entries"]), nil
}
// Is the submitted entry in the allowed list?
if inSlice(i.list, fields[0]) {
return nil, nil
}
return errors.New(errorMessages["in_list"]), nil
}
// -----------------------
type inListMultiple struct {
list []string
}
func InListMultiple(list []string) Rule {
if list == nil {
list = []string{}
}
return &inListMultiple{list}
}
/*
This is used in checkboxes and drop-down multi-select
field []string - must be a slice for multiple entries
list []string - set by the programmer so it must be a slice
return error - FormError with message and extra data if revelant
*/
func (i *inListMultiple) Validate(fields []string, errorMessages map[string]string) (error, []interface{}) {
// if blank, it is fine, use required_multiple
if len(fields) == 0 {
return nil, nil
}
// check for duplicate entries
if hasDuplicates(fields) {
return errors.New(errorMessages["duplicate"]), nil
}
if diff := sliceDiff(fields, i.list); diff == nil {
return nil, nil
}
return errors.New(errorMessages["in_list"]), nil
}
// -----------------------
type notInListSingle struct {
list []string
}
func NotInListSingle(list []string) Rule {
if list == nil {
list = []string{}
}
return ¬InListSingle{list}
}
/*
field string - only one item is permitted
list []string - set by the programmer so it must be a slice
return error - FormError with message and extra data if revelant
*/
func (i *notInListSingle) Validate(fields []string, errorMessages map[string]string) (error, []interface{}) {
field := getFirstKey(fields)
// if blank, it is fine, use required_single
if len(field) == 0 {
return nil, nil
}
// Is the submitted entry in the allowed list?
if !inSlice(i.list, field) {
return nil, nil
}
return errors.New(errorMessages["not_in_list"]), nil
}
// -----------------------
type countryCode struct {
}
func CountryCode() Rule {
return &countryCode{}
}
/*
Expects lowercase two letter country codes
return error - FormError with message and extra data if revelant
*/
func (c *countryCode) Validate(fields []string, errorMessages map[string]string) (error, []interface{}) {
field := getFirstKey(fields)
// if blank, it is fine
if len(field) == 0 {
return nil, nil
}
// check the list
if inSlice(iso3166countryCodes, field) {
return nil, nil
}
return errors.New(errorMessages["country_code"]), nil
}
// -----------------------
type csvEntryStrLen struct {
delimiter rune
min uint32
max uint32
}
/*
csv = comma separated values
This splits an string based on some delimiter (default is a comma, but can be different), and checks minlen and maxlen of each entry.
return error - FormError with message and extra data if revelant
*/
func CSVEntryStrLen(delimiter rune, min, max uint32) Rule {
switch delimiter {
case ',':
case '|':
default:
delimiter = ','
}
return &csvEntryStrLen{delimiter, min, max}
}
func (c *csvEntryStrLen) Validate(fields []string, errorMessages map[string]string) (error, []interface{}) {
field := getFirstKey(fields)
// if blank, it is fine
if len(field) == 0 {
return nil, nil
}
r := csv.NewReader(strings.NewReader(field))
r.Comma = c.delimiter
entries, err := r.ReadAll()
if err != nil {
return errors.New(errorMessages["delimiter_min"]), []interface{}{c.min}
}
if len(entries) == 0 {
return errors.New(errorMessages["delimiter_min"]), []interface{}{c.min}
}
for _, e := range entries[0] {
if len(e) == 0 {
continue
}
if len(e) < int(c.min) {
return errors.New(errorMessages["delimiter_min"]), []interface{}{c.min}
}
if len(e) > int(c.max) {
return errors.New(errorMessages["delimiter_max"]), []interface{}{c.max}
}
}
return nil, nil
}
// -----------------------
type notCommonPassword struct {
}
func NotCommonPassword() Rule {
return ¬CommonPassword{}
}
/*
return error - FormError with message and extra data if revelant
*/
func (n *notCommonPassword) Validate(fields []string, errorMessages map[string]string) (error, []interface{}) {
field := getFirstKey(fields)
// if blank, it is fine
if len(field) == 0 {
return nil, nil
}
// Is the submitted entry in the allowed list?
if !inSlice(commonPasswordList, field) {
return nil, nil
}
return errors.New(errorMessages["weak_password"]), nil
}
// -----------------------
type currencyCode struct {
}
func CurrencyCode() Rule {
return ¤cyCode{}
}
/*
Expects lowercase two letter country codes
return error - FormError with message and extra data if revelant
*/
func (c *currencyCode) Validate(fields []string, errorMessages map[string]string) (error, []interface{}) {
field := getFirstKey(fields)
// if blank, it is fine
if len(field) == 0 {
return nil, nil
}
// check the list
if inSlice(iso4217currencyCodes, field) {
return nil, nil
}
return errors.New(errorMessages["currency_code"]), nil
}