From dcdfaf36f189478a03bcf1ef5e8fcd9fb95a946a Mon Sep 17 00:00:00 2001 From: miraclesu Date: Sun, 28 Jul 2013 16:59:35 +0800 Subject: [PATCH] Accept parameters more types --- validation/validation.go | 55 ++++++++++++++++++----------------- validation/validation_test.go | 4 +-- validation/validators.go | 3 +- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/validation/validation.go b/validation/validation.go index 9b4e3317..238693d2 100644 --- a/validation/validation.go +++ b/validation/validation.go @@ -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 { diff --git a/validation/validation_test.go b/validation/validation_test.go index b537b786..464d98d7 100644 --- a/validation/validation_test.go +++ b/validation/validation_test.go @@ -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") } } diff --git a/validation/validators.go b/validation/validators.go index b3bbebb3..0ea6de2c 100644 --- a/validation/validators.go +++ b/validation/validators.go @@ -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 {