Skip to content

What is govalid?

govalid is a small, opinionated struct validator for Go. It reads validation rules straight off your struct tags and runs them with reflection — no code generation, no schema files, no third-party dependencies beyond golang.org/x/text for locale handling.

go
type RegisterForm struct {
    Username string `valid:"required;username;minlen:3;maxlen:20" label:"用户名"`
    Email    string `valid:"required;email"                       label:"邮箱"`
    Age      uint   `valid:"required;min:18;max:120"              label:"年龄"`
}

errs, ok := govalid.Check(form)

Design principles

  • Tags are the source of truth. A struct field's rules live next to the field. Reviewers see the constraints without grepping handlers.
  • Composable, not magical. Rules are plain functions in a map. Adding your own is one line. There is no DSL to learn.
  • Safe over clever. Nil pointers, maps, channels, embedded structs and unexported fields are all handled without panicking. The library is fuzz-tested across millions of inputs.
  • Localized first. Chinese and English ship in the box; any locale is pluggable. Per-field labels can themselves be localized via label-en / label-zh / label-… tags.

When to reach for govalid

govalid is at its best for the boring 80% of validation:

  • HTTP request bodies and query parameters
  • Configuration files at startup
  • Domain forms (registration, profile, address books)
  • Cross-field constraints (password confirmation, conditional fields)

For dynamic, schema-on-the-wire validation (think JSON Schema, Protobuf descriptors, runtime-generated rules) you'll want a different tool.

What's next

Released under the MIT License.