Favicon of validator

validator

Go library for validating structs and fields via tags, with cross-field, cross-struct, map/slice diving, and i18n error messages.

validator website screenshot
validator GitHub repository preview

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.

Key features

  • Cross-field and cross-struct validation: compare one field against another (equal, greater than, less than) using tags or custom validator functions.
  • Diving into collections: validate every level of slices, arrays, and maps, including both map keys and values.
  • Custom type handling: works with types implementing the sql driver.Valuer interface or a custom Valuer interface, so it validates underlying values rather than wrapper types.
  • Interface resolution: determines the underlying concrete type behind an interface before validating it.
  • Alias tags: bundle several validation rules into a single custom tag name for reuse across structs.
  • Custom field name extraction: pull field names from JSON tags (or others) so error messages reference the name your API consumers actually see.
  • i18n-aware error messages: translate validation errors into different languages via a separate translations package.
  • Large built-in tag set: hundreds of baked-in tags cover string formats, network addresses, hashes, country/currency codes, UUIDs, credit cards, cron expressions, and more.
  • Gin integration: ships as the default validator for the gin web framework, with documented paths for overriding it.

Ideal use cases

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.

Installation

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.

Frequently asked questions

Share:

Stars
20.2K
Forks
1.5K
Last commit
4 days ago
Repository age
12 years
License
MIT
Self-hosted
No
Activity score
81/100
View Repository
Built with:
Ad
Favicon

 

  
 

Similar to validator

Favicon

 

  
 
Favicon

 

  
 
Favicon