package main
import (
"fmt"
"github.com/a-was/go-validator"
)
type MyStruct struct {
Email string `flags:"required"`
Name string `regex:"^[a-z]+$"`
Min int `min:"10" max:"20"`
Max int `min:"10" max:"20"`
Env string `env:"MYAPP_SETTING" default:"default_value"`
}
func main() {
myStruct := MyStruct{
Email: "",
Name: "invalid-regex@example.com",
Min: 5,
Max: 30,
Env: "",
}
err := validator.Validate(&myStruct)
fmt.Println(err)
// Output:
// Email: required value not filled
// Name: invalid value: invalid-regex@example.com does not match regex ^[a-z]+$
// Min: invalid value: 5, minimum value is 10
// Max: invalid value: 30, maximum value is 20
}Tags can be combined with each other
Takes integer as value
For numeric types (int, uint, etc...) it checks if property value is grater or equal than value specyfied in tag
For string, map, slice types it checks len
Same as min, but checks if property value if lower or equal
Takes regex string as value
Checks if string matches provided regex
Takes list of supported flags as value
required- means that property value cannot be zero-value or nil
Takes environment variable name as value
If property value is zero-value or nil, it sets its value to the value of the environment variable
Takes default value as value
If property value is zero-value or nil, it sets its value to value specyfied in tag