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

Skipping Validatable interface #192

Open rmscoal opened 8 months ago

rmscoal commented 8 months ago

I wanted to validate a struct. For example:

type User struct {
    Name string
    Cars []Car
}

type Car struct {
    ID int
    Name string
}

func (v User) Validate() error {
    return validation.ValidateStruct(&v,
        validation.Field(&v.Name, validation.Required),
        validation.Field(&v.Cars, validation.Length(1, 0)), // car's validation is triggered here... But I don't really care... How can I skip it
    )
}

func (c Car) Validate() error {
    return validation.ValidateStruct(&c,
        validation.Field(&c.Name, validation.Required),
    )
}

As of the documentation, Car struct implements the Validatable interface. Hence, it will be .Validate() will be called. But what if I want to skip it? Just the validation.Length().

Thank you so much.