Skip to content

Validation Sessions

This content is for v0.7. Switch to the latest version for up-to-date documentation.

A Validation session holds one or more validators. You typically build one with:

  • Is(...)
  • Check(...)
  • New()

New() creates an empty session you can populate conditionally.

val := v.New()
if month == 6 {
val.Is(v.Number(day, "month_day").LessOrEqualTo(10))
}
if err := val.ToError(); err != nil {
return err
}

Valid() reports if any errors were recorded.

val := v.Is(v.String("", "name").Not().Blank())
_ = val.Valid() // false

Validation.Is(...) appends validators to an existing session:

val := v.Is(v.String("john", "username").Not().Blank())
val.Is(v.String("single", "status").InSlice([]string{"single", "married"}))