Errors & Output
This content is for v0.7. Switch to the latest version for up-to-date documentation.
ToError() and ToValgoError()
Section titled “ToError() and ToValgoError()”ToError()returns a standarderror.ToValgoError()returns*valgo.Errorfor 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() }}AddErrorMessage(name, message)
Section titled “AddErrorMessage(name, message)”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(other)
Section titled “Merge(other)”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))Custom JSON output (Factory)
Section titled “Custom JSON output (Factory)”If you need a different JSON shape, use a Factory with a custom marshal function. See Localization & Factory.