Validator checks Go struct fields and values using validation tags, including nested maps, slices, arrays, and cross-field rules.
Overview
Validator is a Go package that validates structs and individual fields based on struct tags. It supports cross-field and cross-struct constraints, diving into slices, arrays, and maps including both keys and values.
Before validating interface values it resolves the underlying concrete type. Alias tags map several rules to one name, and field errors can expose custom names such as JSON field names. Messages can be customized and made translation-aware for i18n.
A large catalog of built-in tag validators covers strings, networks, formats, comparisons, files, and conditional required rules. Struct-level custom validation and integration patterns for web frameworks like Gin are documented for adopters.
Callers typically receive either nil, a validation error collection, or invalid input errors, with optional initialization flags recommended for newer default struct-required behavior.
Key features
- Tag-driven struct validation
- Cross-field and cross-struct rules
- Map slice and array diving
- Alias validation tags
- Custom field names in errors
- i18n-friendly error messages
- Extensive built-in validators
Use cases
- Validating API request structs
- Enforcing format tags on strings
- Nested configuration validation
- Localized validation error text