Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added support for float in oneof - issue #1105 #1116

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions baked_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/json"
"fmt"
"io/fs"
"math"
"net"
"net/url"
"os"
Expand Down Expand Up @@ -279,6 +280,8 @@ func isOneOf(fl FieldLevel) bool {
v = strconv.FormatInt(field.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
v = strconv.FormatUint(field.Uint(), 10)
case reflect.Float32, reflect.Float64:
v = strconv.FormatFloat(math.Round(field.Float()), 'f', 0, 64)
default:
panic(fmt.Sprintf("Bad field type %T", field.Interface()))
}
Expand Down
8 changes: 4 additions & 4 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5546,7 +5546,6 @@ func TestOneOfValidation(t *testing.T) {
{f: uint8(6), t: "oneof=5 6"},
{f: uint16(6), t: "oneof=5 6"},
{f: uint32(6), t: "oneof=5 6"},
{f: uint64(6), t: "oneof=5 6"},
}

for _, spec := range passSpecs {
Expand Down Expand Up @@ -5575,17 +5574,18 @@ func TestOneOfValidation(t *testing.T) {
{f: uint16(5), t: "oneof=red green"},
{f: uint32(5), t: "oneof=red green"},
{f: uint64(5), t: "oneof=red green"},
{f: float64(3.14), t: "oneof=red green"},
{f: float32(3.14), t: "oneof=red green"},
}

for _, spec := range failSpecs {
t.Logf("%#v", spec)
errs := validate.Var(spec.f, spec.t)
AssertError(t, errs, "", "", "", "", "oneof")
}

PanicMatches(t, func() {
_ = validate.Var(3.14, "oneof=red green")
}, "Bad field type float64")
_ = validate.Var(complex64(1+2i), "oneof=red green")
}, "Bad field type complex64")
}

func TestBase64Validation(t *testing.T) {
Expand Down