A lightweight, expressive, and extensible validation library for Go — inspired by Laravel’s Validator.
- Rule-based validation like
string
,numeric
,email
,min
,max
,gt
,lt
,boolean
,between
etc. - Chainable and expressive rule definitions
- Easy to extend with custom rules
go get github.com/shivajichalise/validator
import (
"github.com/shivajichalise/validator"
_ "github.com/shivajichalise/validator/rules"
)
data := map[string]any{
"username": "rickastley",
"email": "rick@astley.com",
"age": 21,
"is_admin": false,
"duration": 10,
}
rules := map[string][]string{
"username": {"string", "min:5", "max:20"},
"email": {"email:rfc,dns"},
"age": {"numeric", "gt:18"},
"duration": {"between:9,11"},
}
v := validator.Make(data, rules)
if !v.Validate() {
for field, errs := range v.Errors() {
for _, msg := range errs {
fmt.Printf("%s: %s\n", field, msg)
}
}
}
Rule | Description |
---|---|
string |
Value must be a non-empty string |
min:n |
String length must be ≥ n |
max:n |
String length must be ≤ n |
email |
Validates email with basic, RFC, or DNS check |
numeric |
Accepts int and float |
int |
Value must be an integer |
float64 |
Value must be a float64 |
gt:n |
Value must be greater than n |
lt:n |
Value must be less than n |
boolean |
Value must be a boolean |
between:min,max |
Value must be between min and max |
"username": {"string", "min:5", "max:20"}
"email": {"email:rfc,dns"}
"age": {"numeric", "gt:18"}
"score": {"int", "lt:100"}
"is_admin": {"boolean"}
"duration": {"between:9,11"}
Register a custom rule using:
validator.RegisterRule(MyCustomRule{})
Your rule should implement:
type Rule interface {
Name() string
Validate(field string, value any, params ...string) error
}
This package is well-tested and includes:
string
,min
,max
email
with basic, RFC, DNSnumeric
,int
,float64
gt
,lt
including type strictness and validation chaining- Missing and invalid parameter handling
boolean
between
Run all tests:
go test ./...
We welcome contributions from the community! If you have an idea for a new feature or improvement, please submit a pull request. We also appreciate bug reports and other feedback.
To get started with contributing, simply fork this repository, make your changes, and submit a pull request.
This project is licensed under MIT
Star the repository on Github
Follow on Github