Skip to content

Commit 07cbdd2

Browse files
Dean KarnDean Karn
authored andcommitted
Merge pull request #46 from bluesuncorp/v5-development
V5 development
2 parents d780b12 + 5ab4925 commit 07cbdd2

File tree

4 files changed

+161
-32
lines changed

4 files changed

+161
-32
lines changed

baked_in.go

Lines changed: 64 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,77 @@ import (
55
"net/url"
66
"reflect"
77
"strconv"
8+
"strings"
89
"time"
10+
"unicode/utf8"
911
)
1012

1113
// BakedInValidators is the default map of ValidationFunc
1214
// you can add, remove or even replace items to suite your needs,
1315
// or even disregard and use your own map if so desired.
1416
var BakedInValidators = map[string]Func{
15-
"required": hasValue,
16-
"len": hasLengthOf,
17-
"min": hasMinOf,
18-
"max": hasMaxOf,
19-
"eq": isEq,
20-
"ne": isNe,
21-
"lt": isLt,
22-
"lte": isLte,
23-
"gt": isGt,
24-
"gte": isGte,
25-
"eqfield": isEqField,
26-
"nefield": isNeField,
27-
"gtefield": isGteField,
28-
"gtfield": isGtField,
29-
"ltefield": isLteField,
30-
"ltfield": isLtField,
31-
"alpha": isAlpha,
32-
"alphanum": isAlphanum,
33-
"numeric": isNumeric,
34-
"number": isNumber,
35-
"hexadecimal": isHexadecimal,
36-
"hexcolor": isHexcolor,
37-
"rgb": isRgb,
38-
"rgba": isRgba,
39-
"hsl": isHsl,
40-
"hsla": isHsla,
41-
"email": isEmail,
42-
"url": isURL,
43-
"uri": isURI,
44-
"base64": isBase64,
17+
"required": hasValue,
18+
"len": hasLengthOf,
19+
"min": hasMinOf,
20+
"max": hasMaxOf,
21+
"eq": isEq,
22+
"ne": isNe,
23+
"lt": isLt,
24+
"lte": isLte,
25+
"gt": isGt,
26+
"gte": isGte,
27+
"eqfield": isEqField,
28+
"nefield": isNeField,
29+
"gtefield": isGteField,
30+
"gtfield": isGtField,
31+
"ltefield": isLteField,
32+
"ltfield": isLtField,
33+
"alpha": isAlpha,
34+
"alphanum": isAlphanum,
35+
"numeric": isNumeric,
36+
"number": isNumber,
37+
"hexadecimal": isHexadecimal,
38+
"hexcolor": isHexcolor,
39+
"rgb": isRgb,
40+
"rgba": isRgba,
41+
"hsl": isHsl,
42+
"hsla": isHsla,
43+
"email": isEmail,
44+
"url": isURL,
45+
"uri": isURI,
46+
"base64": isBase64,
47+
"contains": contains,
48+
"containsany": containsAny,
49+
"containsrune": containsRune,
50+
"excludes": excludes,
51+
"excludesall": excludesAll,
52+
"excludesrune": excludesRune,
53+
}
54+
55+
func excludesRune(top interface{}, current interface{}, field interface{}, param string) bool {
56+
return !containsRune(top, current, field, param)
57+
}
58+
59+
func excludesAll(top interface{}, current interface{}, field interface{}, param string) bool {
60+
return !containsAny(top, current, field, param)
61+
}
62+
63+
func excludes(top interface{}, current interface{}, field interface{}, param string) bool {
64+
return !contains(top, current, field, param)
65+
}
66+
67+
func containsRune(top interface{}, current interface{}, field interface{}, param string) bool {
68+
r, _ := utf8.DecodeRuneInString(param)
69+
70+
return strings.ContainsRune(field.(string), r)
71+
}
72+
73+
func containsAny(top interface{}, current interface{}, field interface{}, param string) bool {
74+
return strings.ContainsAny(field.(string), param)
75+
}
76+
77+
func contains(top interface{}, current interface{}, field interface{}, param string) bool {
78+
return strings.Contains(field.(string), param)
4579
}
4680

4781
func isNeField(top interface{}, current interface{}, field interface{}, param string) bool {

doc.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,30 @@ Here is a list of the current built in validators:
333333
as an error, if you wish to accept an empty string as valid you can use
334334
this with the omitempty tag. (Usage: base64)
335335
336+
contains
337+
This validates that a string value contains the substring value.
338+
(Usage: contains=@)
339+
340+
containsany
341+
This validates that a string value contains any Unicode code points
342+
in the substring value. (Usage: containsany=!@#?)
343+
344+
containsrune
345+
This validates that a string value contains the supplied rune value.
346+
(Usage: containsrune=@)
347+
348+
excludes
349+
This validates that a string value does not contain the substring value.
350+
(Usage: excludes=@)
351+
352+
excludesall
353+
This validates that a string value does not contain any Unicode code
354+
points in the substring value. (Usage: excludesall=!@#?)
355+
356+
excludesrune
357+
This validates that a string value does not contain the supplied rune value.
358+
(Usage: excludesrune=@)
359+
336360
Validator notes:
337361
338362
regex

validator.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
tagKeySeparator = "="
2626
structOnlyTag = "structonly"
2727
omitempty = "omitempty"
28-
fieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag\n"
28+
fieldErrMsg = "Field validation for \"%s\" failed on the \"%s\" tag"
2929
structErrMsg = "Struct:%s\n"
3030
)
3131

@@ -65,12 +65,13 @@ func (e *StructErrors) Error() string {
6565

6666
for _, err := range e.Errors {
6767
buff.WriteString(err.Error())
68+
buff.WriteString("\n")
6869
}
6970

7071
for _, err := range e.StructErrors {
7172
buff.WriteString(err.Error())
7273
}
73-
buff.WriteString("\n\n")
74+
7475
return buff.String()
7576
}
7677

validator_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,76 @@ func isEqualFunc(val interface{}, current interface{}, field interface{}, param
142142
return current.(string) == field.(string)
143143
}
144144

145+
func (ms *MySuite) TestExcludesRuneValidation(c *C) {
146+
147+
s := "a☺b☻c☹d"
148+
s2 := "abcd"
149+
150+
err := validate.Field(s, "excludesrune=☻")
151+
c.Assert(err, NotNil)
152+
153+
err = validate.Field(s2, "excludesrune=☻")
154+
c.Assert(err, IsNil)
155+
}
156+
157+
func (ms *MySuite) TestExcludesAllValidation(c *C) {
158+
159+
s := "abcd@!jfk"
160+
s2 := "abcdefg"
161+
162+
err := validate.Field(s, "excludesall=@!{}[]")
163+
c.Assert(err, NotNil)
164+
165+
err = validate.Field(s2, "excludesall=@!{}[]")
166+
c.Assert(err, IsNil)
167+
}
168+
169+
func (ms *MySuite) TestExcludesValidation(c *C) {
170+
171+
s := "abcd@!jfk"
172+
173+
err := validate.Field(s, "excludes=@")
174+
c.Assert(err, NotNil)
175+
176+
err = validate.Field(s, "excludes=q")
177+
c.Assert(err, IsNil)
178+
}
179+
180+
func (ms *MySuite) TestContainsRuneValidation(c *C) {
181+
182+
s := "a☺b☻c☹d"
183+
s2 := "abcd"
184+
185+
err := validate.Field(s, "containsrune=☻")
186+
c.Assert(err, IsNil)
187+
188+
err = validate.Field(s2, "containsrune=☻")
189+
c.Assert(err, NotNil)
190+
}
191+
192+
func (ms *MySuite) TestContainsAnyValidation(c *C) {
193+
194+
s := "abcd@!jfk"
195+
s2 := "abcdefg"
196+
197+
err := validate.Field(s, "containsany=@!{}[]")
198+
c.Assert(err, IsNil)
199+
200+
err = validate.Field(s2, "containsany=@!{}[]")
201+
c.Assert(err, NotNil)
202+
}
203+
204+
func (ms *MySuite) TestContainsValidation(c *C) {
205+
206+
s := "abcd@!jfk"
207+
208+
err := validate.Field(s, "contains=@")
209+
c.Assert(err, IsNil)
210+
211+
err = validate.Field(s, "contains=q")
212+
c.Assert(err, NotNil)
213+
}
214+
145215
func (ms *MySuite) TestIsNeFieldValidation(c *C) {
146216

147217
var j uint64

0 commit comments

Comments
 (0)