Vouch
Vouch
is a lightweight, flexible validation library for Go, allowing you to define and enforce validation rules on struct fields with ease.
Installation
go get git.trcreatives.com/triegebauer/go-vouch
Usage
Defining a Schema
A schema is a map of field names to validation rules.
package main
import (
"fmt"
"git.trcreatives.com/triegebauer/go-vouch"
)
type User struct {
Name string
Age int
Email string
}
func main() {
schema := vouch.Schema{
"Name": vouch.Rules(vouch.Required(), vouch.MinLen(3)),
"Age": vouch.Rules(vouch.MinValue(18)),
"Email": vouch.Rules(vouch.Required(), vouch.Email()),
}
user := User{Name: "", Age: 16, Email: "invalid-email"}
if err := schema.Validate(user); err != nil {
fmt.Println("Validation error:", err)
} else {
fmt.Println("Validation passed!")
}
}
Available Rules
Required()
- Ensures the field is not empty.MinLen(n int)
- Ensures the field has at leastn
characters.MaxLen(n int)
- Ensures the field has at mostn
characters.MinValue(n float64)
- Ensures the number is at leastn
.MaxValue(n float64)
- Ensures the number is at mostn
.Email()
- Ensures the field is a valid email.URL()
- Ensures the field is a valid URL.ContainsUpper()
- Ensures the field contains at least one uppercase letter.ContainsDigit()
- Ensures the field contains at least one digit.- ...and many more!
Contributing
Feel free to open issues or submit pull requests to improve vouch
.
License
MIT License
Languages
Go
100%