asaskevich / govalidator

[Go] Package of validators and sanitizers for strings, numerics, slices and structs
MIT License
6.05k stars 555 forks source link

Problem with capital/lower case struct field. Validator float doesn't support kind float32 for value #265

Open jejefferson opened 6 years ago

jejefferson commented 6 years ago

I have an a problem with validate float field in structure:

type Order struct {
    Total         float32        `db:"total" json:"total" valid:"float,required"`
}

My originally json looks like this:

{ "total": 12.5}

After binding to struct and validation it validator raise an exception:

2018-03-16T00:30:27.600+0300    ERROR   server/responses.go:42  total: Validator float doesn't support kind float32 for value 12.5  {"code": "validation_error", "error": "total: Validator float doesn't support kind float32 for value 12.5", "errorCauses": [{"error": "total: Validator float doesn't support kind float32 for value 12.5"}]}

In delve debugger I really have a float type before validation:

data.Order.Total 12.5

My go version: go1.10 darwin/amd64.

jejefferson commented 6 years ago

Validation error depends on capital/lower case field name of struct. In this example second validation fails:

package main
import (
    "fmt"
    "github.com/asaskevich/govalidator"
)
type StructLower struct {
    total float32 `valid:"float,required"`
}
type StructCapital struct {
    Total float32 `valid:"float,required"`
}
func main() {
    testStruct := StructLower{total: 45.32}
    _, err := govalidator.ValidateStruct(&testStruct)
    if err != nil {
        fmt.Println(err.Error())
    }
    testStruct2 := StructCapital{Total: 53.3535}
    _, err = govalidator.ValidateStruct(&testStruct2)
    if err != nil {
        fmt.Println(err.Error())
    }
}
jasonlam604 commented 6 years ago

Fixed, pull request #268 includes unit tests

sergeyglazyrindev commented 3 years ago

Hello guys! I forked this package cause owner disappeared. Hope, he will be back, but it would be easier to merge these changes back if he is back Link to my repo: create issue there and we'll discuss it.