This document provides guidelines for agents working on this codebase.
gookit/validate is a Go data validation library supporting Maps, Structs, and HTTP Requests with 70+ built-in validators.
go build ./... # Build all packages (~20s)
go vet ./... # Validate code (<1s)
go fmt ./... # Format code (<1s)go test ./... # Run all tests (~5s)
go test -v ./... # Verbose tests
go test -coverprofile="profile.cov" ./... # With coverage
go test -bench=. -benchmem . # Run benchmarks (~3s)go test -v -run TestName ./... # Run specific test by name
go test -v -run TestValidators # Example: run validator testsgolangci-lint run # Full linting (uses .golangci.yml)
go vet ./... # Fallback if golangci-lint fails- Use
go fmt ./...for formatting - Use
goimportsfor import organization (standard library first, then external) - Configure editor to strip unused imports and variables
- Variables/functions:
camelCase(e.g.,newValidation,validators) - Exported types/functions:
PascalCase(e.g.,Validation,Struct(),Map()) - Constants:
PascalCaseorCamelCase(e.g.,Email,UUID) - Private fields:
camelCase(e.g.,data,fieldNames) - Interfaces:
PascalCaseending inerwhere appropriate (e.g.,DataFace,ValidatorFace)
// Type aliases for common maps
type M map[string]any
type MS map[string]string
type SValues map[string][]stringimport (
"bytes"
"fmt"
"net/http"
"reflect"
"regexp"
"strings"
"github.com/gookit/goutil/reflects"
)- Group: stdlib, then external packages
- No blank lines between groups (as seen in codebase)
- Use
errors.New()andfmt.Errorf()for creating errors - Return errors explicitly rather than using sentinel error variables
- Use
err != nilchecks for error handling - Example pattern:
func FromJSONBytes(bs []byte) (*MapData, error) {
mp := map[string]any{}
if err := Unmarshal(bs, &mp); err != nil {
return nil, err
}
return &MapData{Map: mp}, nil
}When adding new validators:
- Follow existing validator function signatures in
validators.go - Use descriptive error messages with
{field}placeholder - Register validators in appropriate maps
- Add tests to corresponding
*_test.gofile - Test both success and failure scenarios
type User struct {
Name string `validate:"required|min_len:2" label:"User Name"`
Email string `validate:"email" label:"Email Address"`
Age int `validate:"required|int|min:1|max:99"`
}validate: Validation rules (pipe-separated)label: Display name for error messagesfilter: Filtering/preprocessing rulesmessage: Custom error messagedefault: Default value
- Test files:
*_test.goin same package - Table-driven tests for multiple test cases:
func TestValidatorName(t *testing.T) {
tests := []struct {
name string
input any
want bool
}{
{"valid email", "test@example.com", true},
{"invalid email", "invalid", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// test logic
})
}
}- Use
github.com/gookit/goutil/testutil/assertfor assertions
- Library is optimized for sub-nanosecond validation
- Pre-compile regex patterns at package level
- Reuse maps/slices when possible
- Run benchmarks when modifying core logic
| File | Purpose |
|---|---|
validate.go |
Main entry points (New, Struct, Map, Request) |
validation.go |
Core Validation struct and logic |
validators.go |
70+ built-in validators |
data_source.go |
Data source abstractions |
filtering.go |
Data filtering/sanitization |
messages.go |
Error messages & i18n |
rule.go |
Rule parsing |
validating.go |
Validation execution |
util.go |
Utility functions |
value.go |
Value type conversion |
- Add function in
validators.go - Register in appropriate validator map
- Add documentation in
docs/validators.md - Add test cases
- Verify with
go test -v -run YourValidator
// Map validation
v := validate.Map(map[string]any{"name": "john"})
v.StringRule("name", "required|min_len:2")
// Struct validation
type User struct {
Name string `validate:"required|min_len:2"`
}
v := validate.Struct(&User{Name: "john"})
ok := v.Validate()- Tests run on Go 1.19, 1.21-1.25
- Coverage reported to coveralls
- golangci-lint v2.x for linting
github.com/gookit/filter- Data filteringgithub.com/gookit/goutil- Utility functionsgolang.org/x/sync- Synchronization primitives