This is a Go library for validating structs and individual fields using struct tags. It's built for Go developers who need to validate incoming data (API payloads, config structs, form input) without hand-writing repetitive checks for every field.
Instead of writing if statements to check required fields, string formats, or ranges, you annotate struct fields with tags like validate:"required,email" and call a single function to get back structured errors.
driver.Valuer interface or a custom Valuer interface, so it validates underlying values rather than wrapper types.Use this library when you're building REST APIs in Go and need to validate incoming JSON payloads against struct definitions, especially when validation rules involve comparing fields against each other (password confirmation, date ranges) or validating nested slices and maps of structs. It also fits config-file validation, where you want typed structs with tag-based rules instead of manual checks scattered through your codebase.
It's a good fit if you're already using gin, since it's the framework's default validator, or if you need multi-language error messages for user-facing validation feedback.
It's not the right tool if your validation logic is highly dynamic and can't be expressed as static struct tags (for example, rules that depend on runtime configuration fetched from a database per request), or if you're not using Go at all. For very simple, one-off checks on a handful of variables, plain if statements may be less overhead than adopting a tagging convention and error-type handling.
Install with go get:
go get github.com/go-playground/validator/v10
Import it in your code:
import "github.com/go-playground/validator/v10"
Create a validator instance, ideally with the WithRequiredStructEnabled option (recommended for new usage since it will become default behavior in a future major version):
validate := validator.New(validator.WithRequiredStructEnabled())
Validate a struct and handle errors by type-checking with errors.As:
err := validate.Struct(mystruct)
var validationErrors validator.ValidationErrors
errors.As(err, &validationErrors)
A nil error means validation passed. A non-nil error is either InvalidValidationError (bad input to the validator itself, rare) or ValidationErrors (the actual field-level failures). The repository's _examples directory has runnable code for simple validation, custom field types, struct-level validation, and translations.