dcdfaf36 by miraclesu

Accept parameters more types

1 parent ae7e3171
...@@ -84,16 +84,19 @@ func (v *Validation) Required(obj interface{}, key string) *ValidationResult { ...@@ -84,16 +84,19 @@ func (v *Validation) Required(obj interface{}, key string) *ValidationResult {
84 return v.apply(Required{key}, obj) 84 return v.apply(Required{key}, obj)
85 } 85 }
86 86
87 func (v *Validation) Min(n int, min int, key string) *ValidationResult { 87 // Test that the obj is greater than min if obj's type is int
88 return v.apply(Min{min, key}, n) 88 func (v *Validation) Min(obj interface{}, min int, key string) *ValidationResult {
89 return v.apply(Min{min, key}, obj)
89 } 90 }
90 91
91 func (v *Validation) Max(n int, max int, key string) *ValidationResult { 92 // Test that the obj is less than max if obj's type is int
92 return v.apply(Max{max, key}, n) 93 func (v *Validation) Max(obj interface{}, max int, key string) *ValidationResult {
94 return v.apply(Max{max, key}, obj)
93 } 95 }
94 96
95 func (v *Validation) Range(n, min, max int, key string) *ValidationResult { 97 // Test that the obj is between mni and max if obj's type is int
96 return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, n) 98 func (v *Validation) Range(obj interface{}, min, max int, key string) *ValidationResult {
99 return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, obj)
97 } 100 }
98 101
99 func (v *Validation) MinSize(obj interface{}, min int, key string) *ValidationResult { 102 func (v *Validation) MinSize(obj interface{}, min int, key string) *ValidationResult {
...@@ -120,45 +123,45 @@ func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult ...@@ -120,45 +123,45 @@ func (v *Validation) AlphaNumeric(obj interface{}, key string) *ValidationResult
120 return v.apply(AlphaNumeric{key}, obj) 123 return v.apply(AlphaNumeric{key}, obj)
121 } 124 }
122 125
123 func (v *Validation) Match(str string, regex *regexp.Regexp, key string) *ValidationResult { 126 func (v *Validation) Match(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult {
124 return v.apply(Match{regex, key}, str) 127 return v.apply(Match{regex, key}, obj)
125 } 128 }
126 129
127 func (v *Validation) NoMatch(str string, regex *regexp.Regexp, key string) *ValidationResult { 130 func (v *Validation) NoMatch(obj interface{}, regex *regexp.Regexp, key string) *ValidationResult {
128 return v.apply(NoMatch{Match{Regexp: regex}, key}, str) 131 return v.apply(NoMatch{Match{Regexp: regex}, key}, obj)
129 } 132 }
130 133
131 func (v *Validation) AlphaDash(str string, key string) *ValidationResult { 134 func (v *Validation) AlphaDash(obj interface{}, key string) *ValidationResult {
132 return v.apply(AlphaDash{NoMatch{Match: Match{Regexp: alphaDashPattern}}, key}, str) 135 return v.apply(AlphaDash{NoMatch{Match: Match{Regexp: alphaDashPattern}}, key}, obj)
133 } 136 }
134 137
135 func (v *Validation) Email(str string, key string) *ValidationResult { 138 func (v *Validation) Email(obj interface{}, key string) *ValidationResult {
136 return v.apply(Email{Match{Regexp: emailPattern}, key}, str) 139 return v.apply(Email{Match{Regexp: emailPattern}, key}, obj)
137 } 140 }
138 141
139 func (v *Validation) IP(str string, key string) *ValidationResult { 142 func (v *Validation) IP(obj interface{}, key string) *ValidationResult {
140 return v.apply(IP{Match{Regexp: ipPattern}, key}, str) 143 return v.apply(IP{Match{Regexp: ipPattern}, key}, obj)
141 } 144 }
142 145
143 func (v *Validation) Base64(str string, key string) *ValidationResult { 146 func (v *Validation) Base64(obj interface{}, key string) *ValidationResult {
144 return v.apply(Base64{Match{Regexp: base64Pattern}, key}, str) 147 return v.apply(Base64{Match{Regexp: base64Pattern}, key}, obj)
145 } 148 }
146 149
147 func (v *Validation) Mobile(str string, key string) *ValidationResult { 150 func (v *Validation) Mobile(obj interface{}, key string) *ValidationResult {
148 return v.apply(Mobile{Match{Regexp: mobilePattern}, key}, str) 151 return v.apply(Mobile{Match{Regexp: mobilePattern}, key}, obj)
149 } 152 }
150 153
151 func (v *Validation) Tel(str string, key string) *ValidationResult { 154 func (v *Validation) Tel(obj interface{}, key string) *ValidationResult {
152 return v.apply(Tel{Match{Regexp: telPattern}, key}, str) 155 return v.apply(Tel{Match{Regexp: telPattern}, key}, obj)
153 } 156 }
154 157
155 func (v *Validation) Phone(str string, key string) *ValidationResult { 158 func (v *Validation) Phone(obj interface{}, key string) *ValidationResult {
156 return v.apply(Phone{Mobile{Match: Match{Regexp: mobilePattern}}, 159 return v.apply(Phone{Mobile{Match: Match{Regexp: mobilePattern}},
157 Tel{Match: Match{Regexp: telPattern}}, key}, str) 160 Tel{Match: Match{Regexp: telPattern}}, key}, obj)
158 } 161 }
159 162
160 func (v *Validation) ZipCode(str string, key string) *ValidationResult { 163 func (v *Validation) ZipCode(obj interface{}, key string) *ValidationResult {
161 return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, str) 164 return v.apply(ZipCode{Match{Regexp: zipCodePattern}, key}, obj)
162 } 165 }
163 166
164 func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult { 167 func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
......
...@@ -61,10 +61,10 @@ func TestRange(t *testing.T) { ...@@ -61,10 +61,10 @@ func TestRange(t *testing.T) {
61 valid := Validation{} 61 valid := Validation{}
62 62
63 if valid.Range(-1, 0, 1, "range0_1").Ok { 63 if valid.Range(-1, 0, 1, "range0_1").Ok {
64 t.Error("-1 is bettween 0 and 1 should be false") 64 t.Error("-1 is between 0 and 1 should be false")
65 } 65 }
66 if !valid.Range(1, 0, 1, "range0_1").Ok { 66 if !valid.Range(1, 0, 1, "range0_1").Ok {
67 t.Error("1 is bettween 0 and 1 should be true") 67 t.Error("1 is between 0 and 1 should be true")
68 } 68 }
69 } 69 }
70 70
......
...@@ -264,8 +264,7 @@ type Match struct { ...@@ -264,8 +264,7 @@ type Match struct {
264 } 264 }
265 265
266 func (m Match) IsSatisfied(obj interface{}) bool { 266 func (m Match) IsSatisfied(obj interface{}) bool {
267 str := obj.(string) 267 return m.Regexp.MatchString(fmt.Sprintf("%v", obj))
268 return m.Regexp.MatchString(str)
269 } 268 }
270 269
271 func (m Match) DefaultMessage() string { 270 func (m Match) DefaultMessage() string {
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!