f4901412 by miraclesu

Add some validate functions

1 parent 0e748c68
...@@ -92,6 +92,10 @@ Struct Tag Functions: ...@@ -92,6 +92,10 @@ Struct Tag Functions:
92 Email 92 Email
93 IP 93 IP
94 Base64 94 Base64
95 Mobile
96 Tel
97 Phone
98 ZipCode
95 99
96 100
97 ## LICENSE 101 ## LICENSE
......
...@@ -144,6 +144,23 @@ func (v *Validation) Base64(str string, key string) *ValidationResult { ...@@ -144,6 +144,23 @@ func (v *Validation) Base64(str string, key string) *ValidationResult {
144 return v.apply(Base64{Match{Regexp: base64Pattern}, key}, str) 144 return v.apply(Base64{Match{Regexp: base64Pattern}, key}, str)
145 } 145 }
146 146
147 func (v *Validation) Mobile(str string, key string) *ValidationResult {
148 return v.apply(Mobile{Match{Regexp: mobilePattern}, key}, str)
149 }
150
151 func (v *Validation) Tel(str string, key string) *ValidationResult {
152 return v.apply(Tel{Match{Regexp: telPattern}, key}, str)
153 }
154
155 func (v *Validation) Phone(str string, key string) *ValidationResult {
156 return v.apply(Phone{Mobile{Match: Match{Regexp: mobilePattern}},
157 Tel{Match: Match{Regexp: telPattern}}, key}, str)
158 }
159
160 func (v *Validation) ZipCode(str string, key string) *ValidationResult {
161 return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, str)
162 }
163
147 func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult { 164 func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
148 if chk.IsSatisfied(obj) { 165 if chk.IsSatisfied(obj) {
149 return &ValidationResult{Ok: true} 166 return &ValidationResult{Ok: true}
......
...@@ -218,6 +218,68 @@ func TestBase64(t *testing.T) { ...@@ -218,6 +218,68 @@ func TestBase64(t *testing.T) {
218 } 218 }
219 } 219 }
220 220
221 func TestMobile(t *testing.T) {
222 valid := Validation{}
223
224 if valid.Mobile("19800008888", "mobile").Ok {
225 t.Error("\"19800008888\" is a valid mobile phone number should be false")
226 }
227 if !valid.Mobile("18800008888", "mobile").Ok {
228 t.Error("\"18800008888\" is a valid mobile phone number should be true")
229 }
230 if !valid.Mobile("18000008888", "mobile").Ok {
231 t.Error("\"18000008888\" is a valid mobile phone number should be true")
232 }
233 if !valid.Mobile("8618300008888", "mobile").Ok {
234 t.Error("\"8618300008888\" is a valid mobile phone number should be true")
235 }
236 if !valid.Mobile("+8614700008888", "mobile").Ok {
237 t.Error("\"+8614700008888\" is a valid mobile phone number should be true")
238 }
239 }
240
241 func TestTel(t *testing.T) {
242 valid := Validation{}
243
244 if valid.Tel("222-00008888", "telephone").Ok {
245 t.Error("\"222-00008888\" is a valid telephone number should be false")
246 }
247 if !valid.Tel("022-70008888", "telephone").Ok {
248 t.Error("\"022-70008888\" is a valid telephone number should be true")
249 }
250 if !valid.Tel("02270008888", "telephone").Ok {
251 t.Error("\"02270008888\" is a valid telephone number should be true")
252 }
253 if !valid.Tel("70008888", "telephone").Ok {
254 t.Error("\"70008888\" is a valid telephone number should be true")
255 }
256 }
257
258 func TestPhone(t *testing.T) {
259 valid := Validation{}
260
261 if valid.Phone("222-00008888", "phone").Ok {
262 t.Error("\"222-00008888\" is a valid phone number should be false")
263 }
264 if !valid.Mobile("+8614700008888", "phone").Ok {
265 t.Error("\"+8614700008888\" is a valid phone number should be true")
266 }
267 if !valid.Tel("02270008888", "phone").Ok {
268 t.Error("\"02270008888\" is a valid phone number should be true")
269 }
270 }
271
272 func TestZipCode(t *testing.T) {
273 valid := Validation{}
274
275 if valid.ZipCode("", "zipcode").Ok {
276 t.Error("\"00008888\" is a valid zipcode should be false")
277 }
278 if !valid.ZipCode("536000", "zipcode").Ok {
279 t.Error("\"536000\" is a valid zipcode should be true")
280 }
281 }
282
221 func TestValid(t *testing.T) { 283 func TestValid(t *testing.T) {
222 type user struct { 284 type user struct {
223 Id int 285 Id int
......
...@@ -353,3 +353,70 @@ func (b Base64) DefaultMessage() string { ...@@ -353,3 +353,70 @@ func (b Base64) DefaultMessage() string {
353 func (b Base64) GetKey() string { 353 func (b Base64) GetKey() string {
354 return b.Key 354 return b.Key
355 } 355 }
356
357 // just for chinese mobile phone number
358 var mobilePattern = regexp.MustCompile("^((\\+86)|(86))?(1(([35][0-9])|(47)|[8][01236789]))\\d{8}$")
359
360 type Mobile struct {
361 Match
362 Key string
363 }
364
365 func (m Mobile) DefaultMessage() string {
366 return fmt.Sprint("Must be valid mobile number")
367 }
368
369 func (m Mobile) GetKey() string {
370 return m.Key
371 }
372
373 // just for chinese telephone number
374 var telPattern = regexp.MustCompile("^(0\\d{2,3}(\\-)?)?\\d{7,8}$")
375
376 type Tel struct {
377 Match
378 Key string
379 }
380
381 func (t Tel) DefaultMessage() string {
382 return fmt.Sprint("Must be valid telephone number")
383 }
384
385 func (t Tel) GetKey() string {
386 return t.Key
387 }
388
389 // just for chinese telephone or mobile phone number
390 type Phone struct {
391 Mobile
392 Tel
393 Key string
394 }
395
396 func (p Phone) IsSatisfied(obj interface{}) bool {
397 return p.Mobile.IsSatisfied(obj) || p.Tel.IsSatisfied(obj)
398 }
399
400 func (p Phone) DefaultMessage() string {
401 return fmt.Sprint("Must be valid telephone or mobile phone number")
402 }
403
404 func (p Phone) GetKey() string {
405 return p.Key
406 }
407
408 // just for chinese zipcode
409 var zipCodePattern = regexp.MustCompile("^[1-9]\\d{5}$")
410
411 type ZipCode struct {
412 Match
413 Key string
414 }
415
416 func (z ZipCode) DefaultMessage() string {
417 return fmt.Sprint("Must be valid zipcode")
418 }
419
420 func (z ZipCode) GetKey() string {
421 return z.Key
422 }
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!