68 lines
1.5 KiB
Markdown
68 lines
1.5 KiB
Markdown
# Vouch
|
|
|
|
`Vouch` is a lightweight, flexible validation library for Go, allowing you to define and enforce validation rules on struct fields with ease.
|
|
|
|
## Installation
|
|
|
|
```sh
|
|
go get git.trcreatives.com/triegebauer/go-vouch
|
|
```
|
|
|
|
## Usage
|
|
|
|
### Defining a Schema
|
|
|
|
A schema is a map of field names to validation rules.
|
|
|
|
```go
|
|
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 least `n` characters.
|
|
- `MaxLen(n int)` - Ensures the field has at most `n` characters.
|
|
- `MinValue(n float64)` - Ensures the number is at least `n`.
|
|
- `MaxValue(n float64)` - Ensures the number is at most `n`.
|
|
- `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
|