mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 12:20:54 +00:00
Add some validate functions
This commit is contained in:
parent
0e748c6871
commit
f490141217
@ -92,6 +92,10 @@ Struct Tag Functions:
|
|||||||
Email
|
Email
|
||||||
IP
|
IP
|
||||||
Base64
|
Base64
|
||||||
|
Mobile
|
||||||
|
Tel
|
||||||
|
Phone
|
||||||
|
ZipCode
|
||||||
|
|
||||||
|
|
||||||
## LICENSE
|
## LICENSE
|
||||||
|
@ -144,6 +144,23 @@ func (v *Validation) Base64(str string, key string) *ValidationResult {
|
|||||||
return v.apply(Base64{Match{Regexp: base64Pattern}, key}, str)
|
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 {
|
func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
|
||||||
if chk.IsSatisfied(obj) {
|
if chk.IsSatisfied(obj) {
|
||||||
return &ValidationResult{Ok: true}
|
return &ValidationResult{Ok: true}
|
||||||
|
@ -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) {
|
func TestValid(t *testing.T) {
|
||||||
type user struct {
|
type user struct {
|
||||||
Id int
|
Id int
|
||||||
|
@ -353,3 +353,70 @@ func (b Base64) DefaultMessage() string {
|
|||||||
func (b Base64) GetKey() string {
|
func (b Base64) GetKey() string {
|
||||||
return b.Key
|
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
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user