Required is used to validate if the field value is empty
go get github.com/tiaguinho/required
Simplest way to use the package:
package main
import (
"github.com/tiaguinho/required"
"log"
)
type Test struct {
FirstName string `json:"first_name" required:"-"`
LastName string `json:"last_name" required:"last name is required"`
}
func main() {
t := Test{
FirstName: "Go",
LastName: "Required",
}
if err := required.Validate(t); err != nil {
log.Println(err)
}
}
If you like to get all the validation messages with field's name to return in some API, just change to this:
func main() {
t := Test{
FirstName: "Go",
LastName: "Required",
}
if msg, err := required.ValidateWithMessage(t); err != nil {
log.Println(err, msg)
}
}