Merge pull request #46 from bluesuncorp/v5-development
V5 development
Dean Karn
8 years ago
4 | 4 | "net/url" |
5 | 5 | "reflect" |
6 | 6 | "strconv" |
7 | "strings" | |
7 | 8 | "time" |
9 | "unicode/utf8" | |
8 | 10 | ) |
9 | 11 | |
10 | 12 | // BakedInValidators is the default map of ValidationFunc |
11 | 13 | // you can add, remove or even replace items to suite your needs, |
12 | 14 | // or even disregard and use your own map if so desired. |
13 | 15 | var BakedInValidators = map[string]Func{ |
14 | "required": hasValue, | |
15 | "len": hasLengthOf, | |
16 | "min": hasMinOf, | |
17 | "max": hasMaxOf, | |
18 | "eq": isEq, | |
19 | "ne": isNe, | |
20 | "lt": isLt, | |
21 | "lte": isLte, | |
22 | "gt": isGt, | |
23 | "gte": isGte, | |
24 | "eqfield": isEqField, | |
25 | "nefield": isNeField, | |
26 | "gtefield": isGteField, | |
27 | "gtfield": isGtField, | |
28 | "ltefield": isLteField, | |
29 | "ltfield": isLtField, | |
30 | "alpha": isAlpha, | |
31 | "alphanum": isAlphanum, | |
32 | "numeric": isNumeric, | |
33 | "number": isNumber, | |
34 | "hexadecimal": isHexadecimal, | |
35 | "hexcolor": isHexcolor, | |
36 | "rgb": isRgb, | |
37 | "rgba": isRgba, | |
38 | "hsl": isHsl, | |
39 | "hsla": isHsla, | |
40 | "email": isEmail, | |
41 | "url": isURL, | |
42 | "uri": isURI, | |
43 | "base64": isBase64, | |
16 | "required": hasValue, | |
17 | "len": hasLengthOf, | |
18 | "min": hasMinOf, | |
19 | "max": hasMaxOf, | |
20 | "eq": isEq, | |
21 | "ne": isNe, | |
22 | "lt": isLt, | |
23 | "lte": isLte, | |
24 | "gt": isGt, | |
25 | "gte": isGte, | |
26 | "eqfield": isEqField, | |
27 | "nefield": isNeField, | |
28 | "gtefield": isGteField, | |
29 | "gtfield": isGtField, | |
30 | "ltefield": isLteField, | |
31 | "ltfield": isLtField, | |
32 | "alpha": isAlpha, | |
33 | "alphanum": isAlphanum, | |
34 | "numeric": isNumeric, | |
35 | "number": isNumber, | |
36 | "hexadecimal": isHexadecimal, | |
37 | "hexcolor": isHexcolor, | |
38 | "rgb": isRgb, | |
39 | "rgba": isRgba, | |
40 | "hsl": isHsl, | |
41 | "hsla": isHsla, | |
42 | "email": isEmail, | |
43 | "url": isURL, | |
44 | "uri": isURI, | |
45 | "base64": isBase64, | |
46 | "contains": contains, | |
47 | "containsany": containsAny, | |
48 | "containsrune": containsRune, | |
49 | "excludes": excludes, | |
50 | "excludesall": excludesAll, | |
51 | "excludesrune": excludesRune, | |
52 | } | |
53 | ||
54 | func excludesRune(top interface{}, current interface{}, field interface{}, param string) bool { | |
55 | return !containsRune(top, current, field, param) | |
56 | } | |
57 | ||
58 | func excludesAll(top interface{}, current interface{}, field interface{}, param string) bool { | |
59 | return !containsAny(top, current, field, param) | |
60 | } | |
61 | ||
62 | func excludes(top interface{}, current interface{}, field interface{}, param string) bool { | |
63 | return !contains(top, current, field, param) | |
64 | } | |
65 | ||
66 | func containsRune(top interface{}, current interface{}, field interface{}, param string) bool { | |
67 | r, _ := utf8.DecodeRuneInString(param) | |
68 | ||
69 | return strings.ContainsRune(field.(string), r) | |
70 | } | |
71 | ||
72 | func containsAny(top interface{}, current interface{}, field interface{}, param string) bool { | |
73 | return strings.ContainsAny(field.(string), param) | |
74 | } | |
75 | ||
76 | func contains(top interface{}, current interface{}, field interface{}, param string) bool { | |
77 | return strings.Contains(field.(string), param) | |
44 | 78 | } |
45 | 79 | |
46 | 80 | func isNeField(top interface{}, current interface{}, field interface{}, param string) bool { |
332 | 332 | as an error, if you wish to accept an empty string as valid you can use |
333 | 333 | this with the omitempty tag. (Usage: base64) |
334 | 334 | |
335 | contains | |
336 | This validates that a string value contains the substring value. | |
337 | (Usage: contains=@) | |
338 | ||
339 | containsany | |
340 | This validates that a string value contains any Unicode code points | |
341 | in the substring value. (Usage: containsany=!@#?) | |
342 | ||
343 | containsrune | |
344 | This validates that a string value contains the supplied rune value. | |
345 | (Usage: containsrune=@) | |
346 | ||
347 | excludes | |
348 | This validates that a string value does not contain the substring value. | |
349 | (Usage: excludes=@) | |
350 | ||
351 | excludesall | |
352 | This validates that a string value does not contain any Unicode code | |
353 | points in the substring value. (Usage: excludesall=!@#?) | |
354 | ||
355 | excludesrune | |
356 | This validates that a string value does not contain the supplied rune value. | |
357 | (Usage: excludesrune=@) | |
358 | ||
335 | 359 | Validator notes: |
336 | 360 | |
337 | 361 | regex |
24 | 24 | tagKeySeparator = "=" |
25 | 25 | structOnlyTag = "structonly" |
26 | 26 | omitempty = "omitempty" |
27 | fieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n" | |
27 | fieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag" | |
28 | 28 | structErrMsg = "Struct:%s\n" |
29 | 29 | ) |
30 | 30 | |
64 | 64 | |
65 | 65 | for _, err := range e.Errors { |
66 | 66 | buff.WriteString(err.Error()) |
67 | buff.WriteString("\n") | |
67 | 68 | } |
68 | 69 | |
69 | 70 | for _, err := range e.StructErrors { |
70 | 71 | buff.WriteString(err.Error()) |
71 | 72 | } |
72 | buff.WriteString("\n\n") | |
73 | ||
73 | 74 | return buff.String() |
74 | 75 | } |
75 | 76 |
139 | 139 | func isEqualFunc(val interface{}, current interface{}, field interface{}, param string) bool { |
140 | 140 | |
141 | 141 | return current.(string) == field.(string) |
142 | } | |
143 | ||
144 | func (ms *MySuite) TestExcludesRuneValidation(c *C) { | |
145 | ||
146 | s := "a☺b☻c☹d" | |
147 | s2 := "abcd" | |
148 | ||
149 | err := validate.Field(s, "excludesrune=☻") | |
150 | c.Assert(err, NotNil) | |
151 | ||
152 | err = validate.Field(s2, "excludesrune=☻") | |
153 | c.Assert(err, IsNil) | |
154 | } | |
155 | ||
156 | func (ms *MySuite) TestExcludesAllValidation(c *C) { | |
157 | ||
158 | s := "abcd@!jfk" | |
159 | s2 := "abcdefg" | |
160 | ||
161 | err := validate.Field(s, "excludesall=@!{}[]") | |
162 | c.Assert(err, NotNil) | |
163 | ||
164 | err = validate.Field(s2, "excludesall=@!{}[]") | |
165 | c.Assert(err, IsNil) | |
166 | } | |
167 | ||
168 | func (ms *MySuite) TestExcludesValidation(c *C) { | |
169 | ||
170 | s := "abcd@!jfk" | |
171 | ||
172 | err := validate.Field(s, "excludes=@") | |
173 | c.Assert(err, NotNil) | |
174 | ||
175 | err = validate.Field(s, "excludes=q") | |
176 | c.Assert(err, IsNil) | |
177 | } | |
178 | ||
179 | func (ms *MySuite) TestContainsRuneValidation(c *C) { | |
180 | ||
181 | s := "a☺b☻c☹d" | |
182 | s2 := "abcd" | |
183 | ||
184 | err := validate.Field(s, "containsrune=☻") | |
185 | c.Assert(err, IsNil) | |
186 | ||
187 | err = validate.Field(s2, "containsrune=☻") | |
188 | c.Assert(err, NotNil) | |
189 | } | |
190 | ||
191 | func (ms *MySuite) TestContainsAnyValidation(c *C) { | |
192 | ||
193 | s := "abcd@!jfk" | |
194 | s2 := "abcdefg" | |
195 | ||
196 | err := validate.Field(s, "containsany=@!{}[]") | |
197 | c.Assert(err, IsNil) | |
198 | ||
199 | err = validate.Field(s2, "containsany=@!{}[]") | |
200 | c.Assert(err, NotNil) | |
201 | } | |
202 | ||
203 | func (ms *MySuite) TestContainsValidation(c *C) { | |
204 | ||
205 | s := "abcd@!jfk" | |
206 | ||
207 | err := validate.Field(s, "contains=@") | |
208 | c.Assert(err, IsNil) | |
209 | ||
210 | err = validate.Field(s, "contains=q") | |
211 | c.Assert(err, NotNil) | |
142 | 212 | } |
143 | 213 | |
144 | 214 | func (ms *MySuite) TestIsNeFieldValidation(c *C) { |