Skip to content

Check

go
func Check(v interface{}, lang ...language.Tag) (errs []*ErrContext, ok bool)

Validate v against its struct tags and (optionally) its Validate() error method. Returns the list of failures and an ok flag that's true only when no failures occurred.

Inputs

ParamNotes
vStruct, struct pointer, or slice of structs. Other kinds (maps, ints, …) return (nil, true).
langOptional locale. First value wins; the rest are ignored. Defaults to language.Chinese.

Behavior

  1. Nil-safe. nil and typed-nil pointers return (nil, true).
  2. Pointer-aware. Pointers are dereferenced; the original value's method set is captured first so Validate() works whether you pass the value or a pointer.
  3. Recursive. Nested structs and slices of structs are walked automatically (see Nested Structs & Slices).
  4. Tag-driven. Each rule from valid:"…" runs against the field's value, using Checkers to resolve the checker function.
  5. Validate hook. After tag rules, if v (or its pointer) has a Validate() error, govalid invokes it and appends any non-nil error.

Examples

Basic

go
errs, ok := govalid.Check(form)

Locale per call

go
errs, ok := govalid.Check(form, language.English)

Slice of structs

go
items := []Item{{Name: ""}, {Name: "ok"}}
errs, ok := govalid.Check(items)

Nil pointer

go
var f *Form
errs, ok := govalid.Check(f) // returns (nil, true)

Return value

errs is a slice of *ErrContext, each implementing error. Order is:

  1. Top-level field declaration order.
  2. Within a slice-of-structs field, element order.
  3. Within an element, field declaration order.
  4. Validate() errors come last.

Released under the MIT License.