9 "gopkg.in/go-playground/validator.v9"
12 // DbBackedUser User struct
13 type DbBackedUser struct {
14 Name sql.NullString `validate:"required"`
15 Age sql.NullInt64 `validate:"required"`
18 // use a single instance of Validate, it caches struct info
19 var validate *validator.Validate
23 validate = validator.New()
25 // register all sql.Null* types to use the ValidateValuer CustomTypeFunc
26 validate.RegisterCustomTypeFunc(ValidateValuer, sql.NullString{}, sql.NullInt64{}, sql.NullBool{}, sql.NullFloat64{})
28 // build object for validation
29 x := DbBackedUser{Name: sql.NullString{String: "", Valid: true}, Age: sql.NullInt64{Int64: 0, Valid: false}}
31 err := validate.Struct(x)
34 fmt.Printf("Err(s):\n%+v\n", err)
38 // ValidateValuer implements validator.CustomTypeFunc
39 func ValidateValuer(field reflect.Value) interface{} {
41 if valuer, ok := field.Interface().(driver.Valuer); ok {
43 val, err := valuer.Value()
47 // handle the error how you want