GoValid is a lightweight validation library for Golang designed to simplify data structure validation using tags in structs. It supports built-in rules like required
, min
, max
, and email
, and allows users to define custom validation rules.
- β Tag-based validation for structs.
- β
Built-in rules:
required
: Ensures the field is not empty.min
: Minimum value for integers.max
: Maximum value for integers.email
: Validates email format.
- β Support for custom rules.
- β Easy-to-follow documentation and examples.
Install the library using go get
:
go get github.com/harrysan/govalid
Use the validate
tag to define rules for your struct fields.
package main
import (
"fmt"
"github.com/harrysan/govalid"
)
type User struct {
Name string validate:"required,min=3"
Age int validate:"min=18,max=99"
Email string validate:"required,email"
}
func main() {
user := User{
Name: "Jo",
Age: 17,
Email: "invalid-email",
}
errs := govalid.ValidateStruct(user)
if len(errs) > 0 {
fmt.Println("Validation Errors:")
for _, err := range errs {
fmt.Println(err)
}
} else {
fmt.Println("Validation Passed!")
}
}
Output:
Validation Errors:
Field 'Name' failed validation 'min=3': value must be greater than or equal to 3
Field 'Age' failed validation 'min=18': value must be greater than or equal to 18
Field 'Email' failed validation 'email': invalid email format
Use validate_if
to apply rules conditionally based on another field's value.
type User struct {
IsActive bool `validate:"isTrue"`
Reason string `validate_if:"IsActive=true,required"`
}
Use validate
to apply rules on slice and maps.
type DataSlice struct {
Names []string `validate:"slice,required,min=3"`
Age []int `validate:"slice,max=30"`
Email []string `validate:"slice,email"`
}
type DataMap struct {
Names []string `validate:"slice,required,min=3"`
Tags map[string]string `validate:"map,keys=required;min=3,values=required;min=5"`
}
type Address struct {
City string `validate:"required"`
ZipCode string `validate:"required,min=5"`
}
type UserStruct struct {
Name string `validate:"required"`
Age int `validate:"min=18"`
Address Address `validate:"struct"`
}
type UserCustomError struct {
Age int `validate:"min=18" error_message:"Age must be at least 18"`
Email string `validate:"regex=email" error_message:"Invalid email format"`
}
You can register custom validation rules using the RegisterCustomRule
function.
package main
import (
"fmt"
"github.com/harrysan/govalid"
)
func main() {
// Register a custom rule
err := govalid.RegisterCustomRule("isEven", func(field string, value interface{}) error {
v, ok := value.(int)
if !ok {
return fmt.Errorf("%s must be an integer", field)
}
if v%2 != 0 {
return fmt.Errorf("%s must be an even number", field)
}
return nil
})
if err != nil {
fmt.Println("Failed to register custom rule:", err)
return
}
type Data struct {
Number int validate:"isEven"
}
data := Data{Number: 3}
errs := govalid.ValidateStruct(data)
if len(errs) > 0 {
fmt.Println("Validation Errors:")
for _, err := range errs {
fmt.Println(err)
}
} else {
fmt.Println("Validation Passed!")
}
}
Output:
Validation Errors:
Field 'Number' failed validation 'isEven': Number must be an even number
Rule | Description | Example Tag |
---|---|---|
required |
Ensures the field is not empty. | validate:"required" |
min |
The field must be greater than or equal to a minimum value. | validate:"min=3" |
max |
The field must be less than or equal to a maximum value. | validate:"max=10" |
bool |
The field must be true / false. | validate:"isTrue" validate:"isFalse" |
email |
The field must be in a valid email format. | validate:"email" |
regex |
Regex validation, rules in rules/regex_rules.go for customΒ rules.AddOrUpdateRegexRule (see validate_regex_test.go ) |
validate:"regex=username" |
func ValidateStruct(s interface{}) []ValidationError
func RegisterCustomRule(name string, rule CustomRule) error
Registers a custom validation rule with a unique name and a function that implements the rule.
Struct representing a validation error:
type ValidationError struct {
Field string // Name of the field that failed validation
Tag string // The validation rule that failed
Value interface{} // The value of the field that failed validation
Err error // Details about the error
}
.
βββ govalid/
βββ go.mod
βββ validator/
β βββ validator.go # Core validation logic
β βββ custom.go # Custom rule support
βββ rules/
β βββ rules.go # Rules for validation
β βββ rules_if.go # Rules for validation_if
β βββ regex_rules.go # Regex rules
βββ test/
β βββ validate_string_test.go # Example usage
β βββ validate_custom_test.go # Example usage
β βββ validate_struct_test.go # Example usage
β βββ etc..
βββ README.md # Documentation
Planned features for future updates:
- Support for nested struct validation.
- Customizable error messages (including multi-language support).
- Validation for additional data types (e.g., float, time).
Contributions are welcome! Feel free to open a pull request or report issues in the issues section.
This library is licensed under the MIT License.
Happy coding ! π