Skip to content

Errors & Output

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

  • ToError() returns a standard error.
  • ToValgoError() returns *valgo.Error for structured access.
val := v.Is(v.String("", "name").Not().Blank())
if err := val.ToError(); err != nil {
return err
}
if errInfo := val.ToValgoError(); errInfo != nil {
for field, valueErr := range errInfo.Errors() {
_ = field
_ = valueErr.Messages()
}
}

Attach an error without a validator (useful for “entity-level” errors).

val := v.Is(
v.String(a.City, "city").Not().Blank(),
v.String(a.Street, "street").Not().Blank(),
)
if !val.Valid() {
val.AddErrorMessage("address", "The address is wrong!")
}

Merge two sessions to reuse validation logic.

val := v.Is(
v.String(r.Name, "name").Not().Blank(),
v.String(r.Status, "status").Not().Blank(),
)
val.Merge(validatePreStatus(r.Status))

If you need a different JSON shape, use a Factory with a custom marshal function. See Localization & Factory.