@@ -2,35 +2,77 @@ package validator
22
33import (
44 "fmt"
5+ "net/url"
56 "reflect"
67 "strconv"
78)
89
910// BakedInValidators is the map of ValidationFunc used internally
1011// but can be used with any new Validator if desired
1112var BakedInValidators = map [string ]ValidationFunc {
12- "required" : required ,
13- "len" : length ,
14- "min" : min ,
15- "max" : max ,
16- "lt" : lt ,
17- "lte" : lte ,
18- "gt" : gt ,
19- "gte" : gte ,
20- "alpha" : alpha ,
21- "alphanum" : alphanum ,
22- "numeric" : numeric ,
23- "number" : number ,
24- "hexadecimal" : hexadecimal ,
25- "hexcolor" : hexcolor ,
26- "rgb" : rgb ,
27- "rgba" : rgba ,
28- "hsl" : hsl ,
29- "hsla" : hsla ,
30- "email" : email ,
13+ "required" : hasValue ,
14+ "len" : hasLengthOf ,
15+ "min" : hasMinOf ,
16+ "max" : hasMaxOf ,
17+ "lt" : isLt ,
18+ "lte" : isLte ,
19+ "gt" : isGt ,
20+ "gte" : isGte ,
21+ "alpha" : isAlpha ,
22+ "alphanum" : isAlphanum ,
23+ "numeric" : isNumeric ,
24+ "number" : isNumber ,
25+ "hexadecimal" : isHexadecimal ,
26+ "hexcolor" : isHexcolor ,
27+ "rgb" : isRgb ,
28+ "rgba" : isRgba ,
29+ "hsl" : isHsl ,
30+ "hsla" : isHsla ,
31+ "email" : isEmail ,
32+ "url" : isURL ,
33+ "uri" : isURI ,
3134}
3235
33- func email (field interface {}, param string ) bool {
36+ func isURI (field interface {}, param string ) bool {
37+
38+ st := reflect .ValueOf (field )
39+
40+ switch st .Kind () {
41+
42+ case reflect .String :
43+ _ , err := url .ParseRequestURI (field .(string ))
44+
45+ return err == nil
46+ default :
47+ panic (fmt .Sprintf ("Bad field type %T" , field ))
48+ }
49+ }
50+
51+ func isURL (field interface {}, param string ) bool {
52+
53+ st := reflect .ValueOf (field )
54+
55+ switch st .Kind () {
56+
57+ case reflect .String :
58+ url , err := url .ParseRequestURI (field .(string ))
59+
60+ if err != nil {
61+ return false
62+ }
63+
64+ if len (url .Scheme ) == 0 {
65+ return false
66+ }
67+
68+ return err == nil
69+
70+ default :
71+ panic (fmt .Sprintf ("Bad field type %T" , field ))
72+ }
73+ }
74+
75+ func isEmail (field interface {}, param string ) bool {
3476
3577 st := reflect .ValueOf (field )
3678
@@ -43,7 +85,7 @@ func email(field interface{}, param string) bool {
4385 }
4486}
4587
46- func hsla (field interface {}, param string ) bool {
88+ func isHsla (field interface {}, param string ) bool {
4789
4890 st := reflect .ValueOf (field )
4991
@@ -56,7 +98,7 @@ func hsla(field interface{}, param string) bool {
5698 }
5799}
58100
59- func hsl (field interface {}, param string ) bool {
101+ func isHsl (field interface {}, param string ) bool {
60102
61103 st := reflect .ValueOf (field )
62104
@@ -69,7 +111,7 @@ func hsl(field interface{}, param string) bool {
69111 }
70112}
71113
72- func rgba (field interface {}, param string ) bool {
114+ func isRgba (field interface {}, param string ) bool {
73115
74116 st := reflect .ValueOf (field )
75117
@@ -82,7 +124,7 @@ func rgba(field interface{}, param string) bool {
82124 }
83125}
84126
85- func rgb (field interface {}, param string ) bool {
127+ func isRgb (field interface {}, param string ) bool {
86128
87129 st := reflect .ValueOf (field )
88130
@@ -95,7 +137,7 @@ func rgb(field interface{}, param string) bool {
95137 }
96138}
97139
98- func hexcolor (field interface {}, param string ) bool {
140+ func isHexcolor (field interface {}, param string ) bool {
99141
100142 st := reflect .ValueOf (field )
101143
@@ -108,7 +150,7 @@ func hexcolor(field interface{}, param string) bool {
108150 }
109151}
110152
111- func hexadecimal (field interface {}, param string ) bool {
153+ func isHexadecimal (field interface {}, param string ) bool {
112154
113155 st := reflect .ValueOf (field )
114156
@@ -121,7 +163,7 @@ func hexadecimal(field interface{}, param string) bool {
121163 }
122164}
123165
124- func number (field interface {}, param string ) bool {
166+ func isNumber (field interface {}, param string ) bool {
125167
126168 st := reflect .ValueOf (field )
127169
@@ -134,7 +176,7 @@ func number(field interface{}, param string) bool {
134176 }
135177}
136178
137- func numeric (field interface {}, param string ) bool {
179+ func isNumeric (field interface {}, param string ) bool {
138180
139181 st := reflect .ValueOf (field )
140182
@@ -147,7 +189,7 @@ func numeric(field interface{}, param string) bool {
147189 }
148190}
149191
150- func alphanum (field interface {}, param string ) bool {
192+ func isAlphanum (field interface {}, param string ) bool {
151193
152194 st := reflect .ValueOf (field )
153195
@@ -160,7 +202,7 @@ func alphanum(field interface{}, param string) bool {
160202 }
161203}
162204
163- func alpha (field interface {}, param string ) bool {
205+ func isAlpha (field interface {}, param string ) bool {
164206
165207 st := reflect .ValueOf (field )
166208
@@ -173,7 +215,7 @@ func alpha(field interface{}, param string) bool {
173215 }
174216}
175217
176- func required (field interface {}, param string ) bool {
218+ func hasValue (field interface {}, param string ) bool {
177219
178220 st := reflect .ValueOf (field )
179221
@@ -187,7 +229,7 @@ func required(field interface{}, param string) bool {
187229 }
188230}
189231
190- func gte (field interface {}, param string ) bool {
232+ func isGte (field interface {}, param string ) bool {
191233
192234 st := reflect .ValueOf (field )
193235
@@ -223,7 +265,7 @@ func gte(field interface{}, param string) bool {
223265 }
224266}
225267
226- func gt (field interface {}, param string ) bool {
268+ func isGt (field interface {}, param string ) bool {
227269
228270 st := reflect .ValueOf (field )
229271
@@ -262,7 +304,7 @@ func gt(field interface{}, param string) bool {
262304// length tests whether a variable's length is equal to a given
263305// value. For strings it tests the number of characters whereas
264306// for maps and slices it tests the number of items.
265- func length (field interface {}, param string ) bool {
307+ func hasLengthOf (field interface {}, param string ) bool {
266308
267309 st := reflect .ValueOf (field )
268310
@@ -302,12 +344,12 @@ func length(field interface{}, param string) bool {
302344// number. For number types, it's a simple lesser-than test; for
303345// strings it tests the number of characters whereas for maps
304346// and slices it tests the number of items.
305- func min (field interface {}, param string ) bool {
347+ func hasMinOf (field interface {}, param string ) bool {
306348
307- return gte (field , param )
349+ return isGte (field , param )
308350}
309351
310- func lte (field interface {}, param string ) bool {
352+ func isLte (field interface {}, param string ) bool {
311353
312354 st := reflect .ValueOf (field )
313355
@@ -343,7 +385,7 @@ func lte(field interface{}, param string) bool {
343385 }
344386}
345387
346- func lt (field interface {}, param string ) bool {
388+ func isLt (field interface {}, param string ) bool {
347389
348390 st := reflect .ValueOf (field )
349391
@@ -383,9 +425,9 @@ func lt(field interface{}, param string) bool {
383425// value. For numbers, it's a simple lesser-than test; for
384426// strings it tests the number of characters whereas for maps
385427// and slices it tests the number of items.
386- func max (field interface {}, param string ) bool {
428+ func hasMaxOf (field interface {}, param string ) bool {
387429
388- return lte (field , param )
430+ return isLte (field , param )
389431}
390432
391433// asInt retuns the parameter as a int64
0 commit comments