Skip to content

Commit

Permalink
fix float cast precicion in a few validations (#1146)
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn authored Aug 17, 2023
1 parent 5bf55dc commit 3094d59
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 17 deletions.
62 changes: 48 additions & 14 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -1295,8 +1295,13 @@ func isEq(fl FieldLevel) bool {

return field.Uint() == p

case reflect.Float32, reflect.Float64:
p := asFloat(param)
case reflect.Float32:
p := asFloat32(param)

return field.Float() == p

case reflect.Float64:
p := asFloat64(param)

return field.Float() == p

Expand Down Expand Up @@ -1760,8 +1765,11 @@ func requireCheckFieldValue(
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return field.Uint() == asUint(value)

case reflect.Float32, reflect.Float64:
return field.Float() == asFloat(value)
case reflect.Float32:
return field.Float() == asFloat32(value)

case reflect.Float64:
return field.Float() == asFloat64(value)

case reflect.Slice, reflect.Map, reflect.Array:
return int64(field.Len()) == asInt(value)
Expand Down Expand Up @@ -2060,8 +2068,13 @@ func isGte(fl FieldLevel) bool {

return field.Uint() >= p

case reflect.Float32, reflect.Float64:
p := asFloat(param)
case reflect.Float32:
p := asFloat32(param)

return field.Float() >= p

case reflect.Float64:
p := asFloat64(param)

return field.Float() >= p

Expand Down Expand Up @@ -2106,10 +2119,16 @@ func isGt(fl FieldLevel) bool {

return field.Uint() > p

case reflect.Float32, reflect.Float64:
p := asFloat(param)
case reflect.Float32:
p := asFloat32(param)

return field.Float() > p

case reflect.Float64:
p := asFloat64(param)

return field.Float() > p

case reflect.Struct:

if field.Type().ConvertibleTo(timeType) {
Expand Down Expand Up @@ -2148,8 +2167,13 @@ func hasLengthOf(fl FieldLevel) bool {

return field.Uint() == p

case reflect.Float32, reflect.Float64:
p := asFloat(param)
case reflect.Float32:
p := asFloat32(param)

return field.Float() == p

case reflect.Float64:
p := asFloat64(param)

return field.Float() == p
}
Expand Down Expand Up @@ -2281,8 +2305,13 @@ func isLte(fl FieldLevel) bool {

return field.Uint() <= p

case reflect.Float32, reflect.Float64:
p := asFloat(param)
case reflect.Float32:
p := asFloat32(param)

return field.Float() <= p

case reflect.Float64:
p := asFloat64(param)

return field.Float() <= p

Expand Down Expand Up @@ -2327,8 +2356,13 @@ func isLt(fl FieldLevel) bool {

return field.Uint() < p

case reflect.Float32, reflect.Float64:
p := asFloat(param)
case reflect.Float32:
p := asFloat32(param)

return field.Float() < p

case reflect.Float64:
p := asFloat64(param)

return field.Float() < p

Expand Down
12 changes: 9 additions & 3 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,13 +261,19 @@ func asUint(param string) uint64 {
return i
}

// asFloat returns the parameter as a float64
// asFloat64 returns the parameter as a float64
// or panics if it can't convert
func asFloat(param string) float64 {

func asFloat64(param string) float64 {
i, err := strconv.ParseFloat(param, 64)
panicIf(err)
return i
}

// asFloat64 returns the parameter as a float64
// or panics if it can't convert
func asFloat32(param string) float64 {
i, err := strconv.ParseFloat(param, 32)
panicIf(err)
return i
}

Expand Down

0 comments on commit 3094d59

Please sign in to comment.