I use Joi to do object schema and data validation in JavaScript. In Golang, I know how to do schema validation, but how do I do data validation?
Is there a Joi equivalient for Golang? I am aware of Godog, but it hasn't reached 1.0 yet.
I use Joi to do object schema and data validation in JavaScript. In Golang, I know how to do schema validation, but how do I do data validation?
Is there a Joi equivalient for Golang? I am aware of Godog, but it hasn't reached 1.0 yet.
Try ozzo-validation https://github.com/go-ozzo/ozzo-validation
package main
import (
"fmt"
"regexp"
"github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
)
type Address struct {
Street string
City string
State string
Zip string
}
func (a Address) Validate() error {
return validation.ValidateStruct(&a,
// Street cannot be empty, and the length must between 5 and 50
validation.Field(&a.Street, validation.Required, validation.Length(5, 50)),
// City cannot be empty, and the length must between 5 and 50
validation.Field(&a.City, validation.Required, validation.Length(5, 50)),
// State cannot be empty, and must be a string consisting of two letters in upper case
validation.Field(&a.State, validation.Required, validation.Match(regexp.MustCompile("^[A-Z]{2}$"))),
// State cannot be empty, and must be a string consisting of five digits
validation.Field(&a.Zip, validation.Required, validation.Match(regexp.MustCompile("^[0-9]{5}$"))),
)
}
func main() {
a := Address{
Street: "123",
City: "Unknown",
State: "Virginia",
Zip: "12345",
}
err := a.Validate()
fmt.Println(err)
// Output:
// Street: the length must be between 5 and 50; State: must be in a valid format.
}