88 "time"
99)
1010
11- // BakedInValidators is the map of ValidationFunc used internally
12- // but can be used with any new Validator if desired
11+ // BakedInValidators is the default map of ValidationFunc
12+ // you can add, remove or even replace items to suite your needs,
13+ // or even disregard and use your own map if so desired.
1314var BakedInValidators = map [string ]ValidationFunc {
1415 "required" : hasValue ,
1516 "len" : hasLengthOf ,
@@ -54,7 +55,6 @@ func isURI(top interface{}, current interface{}, field interface{}, param string
5455}
5556
5657func isURL (top interface {}, current interface {}, field interface {}, param string ) bool {
57-
5858 st := reflect .ValueOf (field )
5959
6060 switch st .Kind () {
@@ -77,146 +77,47 @@ func isURL(top interface{}, current interface{}, field interface{}, param string
7777}
7878
7979func isEmail (top interface {}, current interface {}, field interface {}, param string ) bool {
80-
81- st := reflect .ValueOf (field )
82-
83- switch st .Kind () {
84-
85- case reflect .String :
86- return emailRegex .MatchString (field .(string ))
87- }
88-
89- panic (fmt .Sprintf ("Bad field type %T" , field ))
80+ return matchesRegex (emailRegex , field )
9081}
9182
9283func isHsla (top interface {}, current interface {}, field interface {}, param string ) bool {
93-
94- st := reflect .ValueOf (field )
95-
96- switch st .Kind () {
97-
98- case reflect .String :
99- return hslaRegex .MatchString (field .(string ))
100- }
101-
102- panic (fmt .Sprintf ("Bad field type %T" , field ))
84+ return matchesRegex (hslaRegex , field )
10385}
10486
10587func isHsl (top interface {}, current interface {}, field interface {}, param string ) bool {
106-
107- st := reflect .ValueOf (field )
108-
109- switch st .Kind () {
110-
111- case reflect .String :
112- return hslRegex .MatchString (field .(string ))
113- }
114-
115- panic (fmt .Sprintf ("Bad field type %T" , field ))
88+ return matchesRegex (hslRegex , field )
11689}
11790
11891func isRgba (top interface {}, current interface {}, field interface {}, param string ) bool {
119-
120- st := reflect .ValueOf (field )
121-
122- switch st .Kind () {
123-
124- case reflect .String :
125- return rgbaRegex .MatchString (field .(string ))
126- }
127-
128- panic (fmt .Sprintf ("Bad field type %T" , field ))
92+ return matchesRegex (rgbaRegex , field )
12993}
13094
13195func isRgb (top interface {}, current interface {}, field interface {}, param string ) bool {
132-
133- st := reflect .ValueOf (field )
134-
135- switch st .Kind () {
136-
137- case reflect .String :
138- return rgbRegex .MatchString (field .(string ))
139- }
140-
141- panic (fmt .Sprintf ("Bad field type %T" , field ))
96+ return matchesRegex (rgbRegex , field )
14297}
14398
14499func isHexcolor (top interface {}, current interface {}, field interface {}, param string ) bool {
145-
146- st := reflect .ValueOf (field )
147-
148- switch st .Kind () {
149-
150- case reflect .String :
151- return hexcolorRegex .MatchString (field .(string ))
152- }
153-
154- panic (fmt .Sprintf ("Bad field type %T" , field ))
100+ return matchesRegex (hexcolorRegex , field )
155101}
156102
157103func isHexadecimal (top interface {}, current interface {}, field interface {}, param string ) bool {
158-
159- st := reflect .ValueOf (field )
160-
161- switch st .Kind () {
162-
163- case reflect .String :
164- return hexadecimalRegex .MatchString (field .(string ))
165- }
166-
167- panic (fmt .Sprintf ("Bad field type %T" , field ))
104+ return matchesRegex (hexadecimalRegex , field )
168105}
169106
170107func isNumber (top interface {}, current interface {}, field interface {}, param string ) bool {
171-
172- st := reflect .ValueOf (field )
173-
174- switch st .Kind () {
175-
176- case reflect .String :
177- return numberRegex .MatchString (field .(string ))
178- }
179-
180- panic (fmt .Sprintf ("Bad field type %T" , field ))
108+ return matchesRegex (numberRegex , field )
181109}
182110
183111func isNumeric (top interface {}, current interface {}, field interface {}, param string ) bool {
184-
185- st := reflect .ValueOf (field )
186-
187- switch st .Kind () {
188-
189- case reflect .String :
190- return numericRegex .MatchString (field .(string ))
191- }
192-
193- panic (fmt .Sprintf ("Bad field type %T" , field ))
112+ return matchesRegex (numericRegex , field )
194113}
195114
196115func isAlphanum (top interface {}, current interface {}, field interface {}, param string ) bool {
197-
198- st := reflect .ValueOf (field )
199-
200- switch st .Kind () {
201-
202- case reflect .String :
203- return alphaNumericRegex .MatchString (field .(string ))
204- }
205-
206- panic (fmt .Sprintf ("Bad field type %T" , field ))
116+ return matchesRegex (alphaNumericRegex , field )
207117}
208118
209119func isAlpha (top interface {}, current interface {}, field interface {}, param string ) bool {
210-
211- st := reflect .ValueOf (field )
212-
213- switch st .Kind () {
214-
215- case reflect .String :
216- return alphaRegex .MatchString (field .(string ))
217- }
218-
219- panic (fmt .Sprintf ("Bad field type %T" , field ))
120+ return matchesRegex (alphaRegex , field )
220121}
221122
222123func hasValue (top interface {}, current interface {}, field interface {}, param string ) bool {
@@ -767,10 +668,7 @@ func hasMaxOf(top interface{}, current interface{}, field interface{}, param str
767668func asInt (param string ) int64 {
768669
769670 i , err := strconv .ParseInt (param , 0 , 64 )
770-
771- if err != nil {
772- panic (err .Error ())
773- }
671+ panicIf (err )
774672
775673 return i
776674}
@@ -780,10 +678,7 @@ func asInt(param string) int64 {
780678func asUint (param string ) uint64 {
781679
782680 i , err := strconv .ParseUint (param , 0 , 64 )
783-
784- if err != nil {
785- panic (err .Error ())
786- }
681+ panicIf (err )
787682
788683 return i
789684}
@@ -793,10 +688,13 @@ func asUint(param string) uint64 {
793688func asFloat (param string ) float64 {
794689
795690 i , err := strconv .ParseFloat (param , 64 )
691+ panicIf (err )
796692
693+ return i
694+ }
695+
696+ func panicIf (err error ) {
797697 if err != nil {
798698 panic (err .Error ())
799699 }
800-
801- return i
802700}
0 commit comments