go-ozzo / ozzo-validation

An idiomatic Go (golang) validation package. Supports configurable and extensible validation rules (validators) using normal language constructs instead of error-prone struct tags.
MIT License
3.78k stars 224 forks source link

Min() validator fails on uint32 field #51

Closed mklimuk closed 6 years ago

mklimuk commented 6 years ago

I stumbled upon this when trying to validate a struct with uint32 field. The validation fails with

Integer: cannot convert uint32 to int64.

Here's a complete test case:

package main

import (
    "fmt"
    "testing"

    "github.com/go-ozzo/ozzo-validation"
)

type A struct {
    Integer uint32
}

func (a A) Validate() error {
    return validation.ValidateStruct(&a,
        validation.Field(&a.Integer, validation.Required, validation.Min(1)))
}

func TestValidate(t *testing.T) {
    a := A{2}
    err := a.Validate()
    if err != nil {
        fmt.Printf("validation error received: %s", err.Error())
        t.Fail()
    }
}
mklimuk commented 6 years ago

After some digging in I suppose the error is due to the fact that Indirect(value interface{}) (interface{}, bool) method does not resolve pointers to uint32 properly. I'll try to issue a PR if I manage to fix the issue.

mklimuk commented 6 years ago

Ok, my bad. It just requires to set the validator value in the target type. So validation.Min(uint32(1)) fixed it for me. Closing...