From f49014121785b598844cbb7143e6cc00d304cc9b Mon Sep 17 00:00:00 2001 From: miraclesu Date: Sat, 27 Jul 2013 20:40:15 +0800 Subject: [PATCH] Add some validate functions --- validation/README.md | 4 +++ validation/validation.go | 17 +++++++++ validation/validation_test.go | 62 ++++++++++++++++++++++++++++++++ validation/validators.go | 67 +++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+) diff --git a/validation/README.md b/validation/README.md index 9d252799..c436bdd8 100644 --- a/validation/README.md +++ b/validation/README.md @@ -92,6 +92,10 @@ Struct Tag Functions: Email IP Base64 + Mobile + Tel + Phone + ZipCode ## LICENSE diff --git a/validation/validation.go b/validation/validation.go index 46251716..9b4e3317 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -144,6 +144,23 @@ func (v *Validation) Base64(str string, key string) *ValidationResult { return v.apply(Base64{Match{Regexp: base64Pattern}, key}, str) } +func (v *Validation) Mobile(str string, key string) *ValidationResult { + return v.apply(Mobile{Match{Regexp: mobilePattern}, key}, str) +} + +func (v *Validation) Tel(str string, key string) *ValidationResult { + return v.apply(Tel{Match{Regexp: telPattern}, key}, str) +} + +func (v *Validation) Phone(str string, key string) *ValidationResult { + return v.apply(Phone{Mobile{Match: Match{Regexp: mobilePattern}}, + Tel{Match: Match{Regexp: telPattern}}, key}, str) +} + +func (v *Validation) ZipCode(str string, key string) *ValidationResult { + return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, str) +} + func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult { if chk.IsSatisfied(obj) { return &ValidationResult{Ok: true} diff --git a/validation/validation_test.go b/validation/validation_test.go index d9f35922..b537b786 100644 --- a/validation/validation_test.go +++ b/validation/validation_test.go @@ -218,6 +218,68 @@ func TestBase64(t *testing.T) { } } +func TestMobile(t *testing.T) { + valid := Validation{} + + if valid.Mobile("19800008888", "mobile").Ok { + t.Error("\"19800008888\" is a valid mobile phone number should be false") + } + if !valid.Mobile("18800008888", "mobile").Ok { + t.Error("\"18800008888\" is a valid mobile phone number should be true") + } + if !valid.Mobile("18000008888", "mobile").Ok { + t.Error("\"18000008888\" is a valid mobile phone number should be true") + } + if !valid.Mobile("8618300008888", "mobile").Ok { + t.Error("\"8618300008888\" is a valid mobile phone number should be true") + } + if !valid.Mobile("+8614700008888", "mobile").Ok { + t.Error("\"+8614700008888\" is a valid mobile phone number should be true") + } +} + +func TestTel(t *testing.T) { + valid := Validation{} + + if valid.Tel("222-00008888", "telephone").Ok { + t.Error("\"222-00008888\" is a valid telephone number should be false") + } + if !valid.Tel("022-70008888", "telephone").Ok { + t.Error("\"022-70008888\" is a valid telephone number should be true") + } + if !valid.Tel("02270008888", "telephone").Ok { + t.Error("\"02270008888\" is a valid telephone number should be true") + } + if !valid.Tel("70008888", "telephone").Ok { + t.Error("\"70008888\" is a valid telephone number should be true") + } +} + +func TestPhone(t *testing.T) { + valid := Validation{} + + if valid.Phone("222-00008888", "phone").Ok { + t.Error("\"222-00008888\" is a valid phone number should be false") + } + if !valid.Mobile("+8614700008888", "phone").Ok { + t.Error("\"+8614700008888\" is a valid phone number should be true") + } + if !valid.Tel("02270008888", "phone").Ok { + t.Error("\"02270008888\" is a valid phone number should be true") + } +} + +func TestZipCode(t *testing.T) { + valid := Validation{} + + if valid.ZipCode("", "zipcode").Ok { + t.Error("\"00008888\" is a valid zipcode should be false") + } + if !valid.ZipCode("536000", "zipcode").Ok { + t.Error("\"536000\" is a valid zipcode should be true") + } +} + func TestValid(t *testing.T) { type user struct { Id int diff --git a/validation/validators.go b/validation/validators.go index e8533749..b3bbebb3 100644 --- a/validation/validators.go +++ b/validation/validators.go @@ -353,3 +353,70 @@ func (b Base64) DefaultMessage() string { func (b Base64) GetKey() string { return b.Key } + +// just for chinese mobile phone number +var mobilePattern = regexp.MustCompile("^((\\+86)|(86))?(1(([35][0-9])|(47)|[8][01236789]))\\d{8}$") + +type Mobile struct { + Match + Key string +} + +func (m Mobile) DefaultMessage() string { + return fmt.Sprint("Must be valid mobile number") +} + +func (m Mobile) GetKey() string { + return m.Key +} + +// just for chinese telephone number +var telPattern = regexp.MustCompile("^(0\\d{2,3}(\\-)?)?\\d{7,8}$") + +type Tel struct { + Match + Key string +} + +func (t Tel) DefaultMessage() string { + return fmt.Sprint("Must be valid telephone number") +} + +func (t Tel) GetKey() string { + return t.Key +} + +// just for chinese telephone or mobile phone number +type Phone struct { + Mobile + Tel + Key string +} + +func (p Phone) IsSatisfied(obj interface{}) bool { + return p.Mobile.IsSatisfied(obj) || p.Tel.IsSatisfied(obj) +} + +func (p Phone) DefaultMessage() string { + return fmt.Sprint("Must be valid telephone or mobile phone number") +} + +func (p Phone) GetKey() string { + return p.Key +} + +// just for chinese zipcode +var zipCodePattern = regexp.MustCompile("^[1-9]\\d{5}$") + +type ZipCode struct { + Match + Key string +} + +func (z ZipCode) DefaultMessage() string { + return fmt.Sprint("Must be valid zipcode") +} + +func (z ZipCode) GetKey() string { + return z.Key +}