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.76k stars 223 forks source link

Stack overflow after recursive validation #170

Closed silmin closed 2 years ago

silmin commented 2 years ago

Hi! I'm trying to implement a Validate() method on a struct and I'm trying to use a custom validator, but I've come across strange behavior.

When I execute the following code, the validation works again and the stack overflows.

func main() {
    var foo Foo
    foo.str = "foo"
    foo.Validate()
}

type Foo struct {
    str string
}

func (a Foo) Validate() error {
    return validation.Validate(a, validation.By(checkFoo))
}

func checkFoo(i interface{}) error {
    foo, _ := i.(Foo)
    fmt.Println(foo)
    if foo.str != "foo" {
        return fmt.Errorf("must be foo")
    }
    return nil
}

On The Go Playground, I got timeout error with many {foo} https://go.dev/play/p/fMiGwDUiIMN?v=goprev

But if I comment out fmt.Println(foo) on checkFoo() this works fine.

I didn't know the cause. What the hell is this? Is it a bug?

Thank you.

silmin commented 2 years ago

I feel like I understand the cause, so I will close it. I'll implement ValidateStruct() .