1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-22 02:00:55 +00:00

Accept parameters more types

This commit is contained in:
miraclesu 2013-07-28 16:59:35 +08:00
parent ae7e31717a
commit dcdfaf36f1
3 changed files with 32 additions and 30 deletions

View File

@ -84,16 +84,19 @@ func (v *Validation) Required(obj interface{}, key string) *ValidationResult {
return v.apply(Required{key}, obj)
}
func (v *Validation) Min(n int, min int, key string) *ValidationResult {
return v.apply(Min{min, key}, n)
// Test that the obj is greater than min if obj's type is int
func (v *Validation) Min(obj interface{}, min int, key string) *ValidationResult {
return v.apply(Min{min, key}, obj)
}
func (v *Validation) Max(n int, max int, key string) *ValidationResult {
return v.apply(Max{max, key}, n)
// Test that the obj is less than max if obj's type is int
func (v *Validation) Max(obj interface{}, max int, key string) *ValidationResult {
return v.apply(Max{max, key}, obj)
}
func (v *Validation) Range(n, min, max int, key string) *ValidationResult {
return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, n)
// Test that the obj is between mni and max if obj's type is int
func (v *Validation) Range(obj interface{}, min, max int, key string) *ValidationResult {
return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, obj)
}
func (v *Validation) MinSize(obj interface{}, min int, key string) *ValidationResult {
@ -120,45 +123,45 @@ func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult
return v.apply(AlphaNumeric{key}, obj)
}
func (v *Validation) Match(str string, regex *regexp.Regexp, key string) *ValidationResult {
return v.apply(Match{regex, key}, str)
func (v *Validation) Match(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult {
return v.apply(Match{regex, key}, obj)
}
func (v *Validation) NoMatch(str string, regex *regexp.Regexp, key string) *ValidationResult {
return v.apply(NoMatch{Match{Regexp: regex}, key}, str)
func (v *Validation) NoMatch(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult {
return v.apply(NoMatch{Match{Regexp: regex}, key}, obj)
}
func (v *Validation) AlphaDash(str string, key string) *ValidationResult {
return v.apply(AlphaDash{NoMatch{Match: Match{Regexp: alphaDashPattern}}, key}, str)
func (v *Validation) AlphaDash(obj interface{}, key string) *ValidationResult {
return v.apply(AlphaDash{NoMatch{Match: Match{Regexp: alphaDashPattern}}, key}, obj)
}
func (v *Validation) Email(str string, key string) *ValidationResult {
return v.apply(Email{Match{Regexp: emailPattern}, key}, str)
func (v *Validation) Email(obj interface{}, key string) *ValidationResult {
return v.apply(Email{Match{Regexp: emailPattern}, key}, obj)
}
func (v *Validation) IP(str string, key string) *ValidationResult {
return v.apply(IP{Match{Regexp: ipPattern}, key}, str)
func (v *Validation) IP(obj interface{}, key string) *ValidationResult {
return v.apply(IP{Match{Regexp: ipPattern}, key}, obj)
}
func (v *Validation) Base64(str string, key string) *ValidationResult {
return v.apply(Base64{Match{Regexp: base64Pattern}, key}, str)
func (v *Validation) Base64(obj interface{}, key string) *ValidationResult {
return v.apply(Base64{Match{Regexp: base64Pattern}, key}, obj)
}
func (v *Validation) Mobile(str string, key string) *ValidationResult {
return v.apply(Mobile{Match{Regexp: mobilePattern}, key}, str)
func (v *Validation) Mobile(obj interface{}, key string) *ValidationResult {
return v.apply(Mobile{Match{Regexp: mobilePattern}, key}, obj)
}
func (v *Validation) Tel(str string, key string) *ValidationResult {
return v.apply(Tel{Match{Regexp: telPattern}, key}, str)
func (v *Validation) Tel(obj interface{}, key string) *ValidationResult {
return v.apply(Tel{Match{Regexp: telPattern}, key}, obj)
}
func (v *Validation) Phone(str string, key string) *ValidationResult {
func (v *Validation) Phone(obj interface{}, key string) *ValidationResult {
return v.apply(Phone{Mobile{Match: Match{Regexp: mobilePattern}},
Tel{Match: Match{Regexp: telPattern}}, key}, str)
Tel{Match: Match{Regexp: telPattern}}, key}, obj)
}
func (v *Validation) ZipCode(str string, key string) *ValidationResult {
return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, str)
func (v *Validation) ZipCode(obj interface{}, key string) *ValidationResult {
return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, obj)
}
func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {

View File

@ -61,10 +61,10 @@ func TestRange(t *testing.T) {
valid := Validation{}
if valid.Range(-1, 0, 1, "range0_1").Ok {
t.Error("-1 is bettween 0 and 1 should be false")
t.Error("-1 is between 0 and 1 should be false")
}
if !valid.Range(1, 0, 1, "range0_1").Ok {
t.Error("1 is bettween 0 and 1 should be true")
t.Error("1 is between 0 and 1 should be true")
}
}

View File

@ -264,8 +264,7 @@ type Match struct {
}
func (m Match) IsSatisfied(obj interface{}) bool {
str := obj.(string)
return m.Regexp.MatchString(str)
return m.Regexp.MatchString(fmt.Sprintf("%v", obj))
}
func (m Match) DefaultMessage() string {