Skip to content

Commit 95e93c4

Browse files
committed
update
1 parent 86da7bb commit 95e93c4

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

util.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ func magic(t reflect.Type, s string) (i interface{}, err error) {
246246
switch t.Kind() {
247247
case reflect.Int:
248248
i, err = strconv.Atoi(s)
249+
case reflect.Float64:
250+
i, err = strconv.ParseFloat(s, 64)
249251
case reflect.String:
250252
i = s
251253
case reflect.Ptr:

validation.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func (v *Validation) HasErrors() bool {
105105
return len(v.Errors) > 1
106106
}
107107

108-
// Return the errors mapped by key.
108+
// ErrorMap Return the errors mapped by key.
109109
// If there are multiple validation errors associated with a single key, the
110110
// first one "wins". (Typically the first validation will be the more basic).
111111
func (v *Validation) ErrorMap() map[string]*ValidationError {
@@ -119,23 +119,23 @@ func (v *Validation) Error() *ValidationError {
119119
return NoError
120120
}
121121

122-
// Test that the argument is non-nil and non-empty (if string or list)
122+
// Required Test that the argument is non-nil and non-empty (if string or list)
123123
func (v *Validation) Required(obj interface{}, key string) *ValidationResult {
124124
return v.apply(Required{key}, obj)
125125
}
126126

127-
// Test that the obj is greater than min if obj's type is int
128-
func (v *Validation) Min(obj interface{}, min int, key string) *ValidationResult {
127+
// Min Test that the obj is greater than min if obj's type is int
128+
func (v *Validation) Min(obj interface{}, min float64, key string) *ValidationResult {
129129
return v.apply(Min{min, key}, obj)
130130
}
131131

132-
// Test that the obj is less than max if obj's type is int
133-
func (v *Validation) Max(obj interface{}, max int, key string) *ValidationResult {
132+
// Max Test that the obj is less than max if obj's type is int
133+
func (v *Validation) Max(obj interface{}, max float64, key string) *ValidationResult {
134134
return v.apply(Max{max, key}, obj)
135135
}
136136

137-
// Test that the obj is between mni and max if obj's type is int
138-
func (v *Validation) Range(obj interface{}, min, max int, key string) *ValidationResult {
137+
// Range Test that the obj is between mni and max if obj's type is int
138+
func (v *Validation) Range(obj interface{}, min, max float64, key string) *ValidationResult {
139139
return v.apply(Range{Min{Min: min}, Max{Max: max}, key}, obj)
140140
}
141141

validators.go

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -138,16 +138,21 @@ func (r Required) GetLimitValue() interface{} {
138138
}
139139

140140
type Min struct {
141-
Min int
141+
Min float64
142142
Key string
143143
}
144144

145145
func (m Min) IsSatisfied(obj interface{}) bool {
146-
num, ok := obj.(int)
147-
if ok {
146+
switch num := obj.(type) {
147+
case int:
148+
return float64(num) >= m.Min
149+
case int64:
150+
return float64(num) >= m.Min
151+
case float64:
148152
return num >= m.Min
153+
default:
154+
return false
149155
}
150-
return false
151156
}
152157

153158
func (m Min) DefaultMessage() string {
@@ -163,16 +168,21 @@ func (m Min) GetLimitValue() interface{} {
163168
}
164169

165170
type Max struct {
166-
Max int
171+
Max float64
167172
Key string
168173
}
169174

170175
func (m Max) IsSatisfied(obj interface{}) bool {
171-
num, ok := obj.(int)
172-
if ok {
176+
switch num := obj.(type) {
177+
case int:
178+
return float64(num) <= m.Max
179+
case int64:
180+
return float64(num) <= m.Max
181+
case float64:
173182
return num <= m.Max
183+
default:
184+
return false
174185
}
175-
return false
176186
}
177187

178188
func (m Max) DefaultMessage() string {
@@ -187,7 +197,7 @@ func (m Max) GetLimitValue() interface{} {
187197
return m.Max
188198
}
189199

190-
// Requires an integer to be within Min, Max inclusive.
200+
// Range Requires an integer to be within Min, Max inclusive.
191201
type Range struct {
192202
Min
193203
Max
@@ -207,10 +217,10 @@ func (r Range) GetKey() string {
207217
}
208218

209219
func (r Range) GetLimitValue() interface{} {
210-
return []int{r.Min.Min, r.Max.Max}
220+
return []float64{r.Min.Min, r.Max.Max}
211221
}
212222

213-
// Requires an array or string to be at least a given length.
223+
// MinSize Requires an array or string to be at least a given length.
214224
type MinSize struct {
215225
Min int
216226
Key string

0 commit comments

Comments
 (0)