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.73k stars 224 forks source link

Stack overflow when validating struct containing validatable #81

Closed jonasknobloch closed 4 years ago

jonasknobloch commented 5 years ago

example

When validating the struct with validation.ValidateStruct, validation dives into the Foo field and gets trapped in an infinite loop. Not sure if I misinterpreted the docs though. Some clarification if this is intended would be appreciated.

jonasknobloch commented 5 years ago

works as expected with structs

jonasknobloch commented 5 years ago
package main

import (
    "fmt"
    "github.com/go-ozzo/ozzo-validation"
)

type Foo string
type Bar struct {
    A string
    B Foo
}

func (f Foo) Validate() error {
    return validation.Validate(string(f),
        validation.Required,
    )
}

func (b Bar) Validate() error {
    return validation.ValidateStruct(&b,
        validation.Field(
            &b.A,
            validation.Required,
        ),
        validation.Field(&b.B),
    )
}

func main() {   
    var foo Foo = ""
    bar := Bar{"bar", foo}

    fmt.Println(bar.Validate())
}

Doing a type conversion back to string works.

Some clarification if this is intended would be appreciated.

qiangxue commented 4 years ago

Please see the commit for an explanation. Thanks for reporting the issue.