Skip to content

Global struct validators #21

@typerandom

Description

@typerandom

We should have the ability to attach a global validator method on a struct.

Suggestion

A single dumb validator method for validating the struct. Automatically called before all field validators.

type Foo struct {
    IsBraw bool
    Age    int
}

func (f *Foo) Validate() error {
    if f.IsBraw && f.Age >= 120 {
        return errors.New("Braw, you too old.")
    }
    return nil
}
Thoughts
  • Should there be support for only one of these validators? Is there a case where one would want to have multiple methods?
  • Is "Validate" dangerous to use? I.e. when validating nested structures, one could accidentally have a struct referenced with the function "Validate". This could cause strange unpredictable errors... So might be better use the convention/automatically discover methods named "Validate_{Name}" or "ValidateGlobal{Name}".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions