repositorypackage
0.0.0-20241008012400-b1b2d59bb18c
Repository: https://github.com/lufia/go-validator.git
Documentation: pkg.go.dev
# README
go-validator
Yet another validator written in Go.
Features
- strongly-typed validators by type parameters
- i18n support in the standard way
- handling multiple validation errors
Built-in validators
- Required: validates comparable types if the value is not zero-value.
- Length: validates strings if the length of the value is within the range.
- MinLength: see Length.
- MaxLength: see Length.
- InRange: validates ordered types if the value is within the range.
- Min: see InRange.
- Max: see InRange.
- In: validates comparable types if the value is in valid values.
- Pattern: validates strings if it matches the regular expression.
Supported languages
- English
- Japanese
Example
import (
"context"
"fmt"
"github.com/lufia/go-validator"
)
type OIDCProvider int
const (
Google OIDCProvider = iota + 1
Apple
GitHub
)
type CreateUserRequest struct {
Name string
Provider OIDCProvider
Theme string
}
var createUserRequestValidator = validator.Struct(func(s validator.StructRule, r *CreateUserRequest) {
validator.AddField(s, &r.Name, "name", validator.Length[string](5, 20))
validator.AddField(s, &r.Provider, "provider", validator.In(Google, Apple, GitHub))
validator.AddField(s, &r.Theme, "theme", validator.In("light", "dark"))
})
func main() {
var r CreateUserRequest
err := createUserRequestValidator.Validate(context.Background(), &r)
fmt.Println(err)
}
For more details, see the documentation.