go-playground/validator.v9を使って、CustomバリデーションはMininのStructの場合、メモしました。
package main
import (
"fmt"
"gopkg.in/go-playground/validator.v9"
)
type A struct {
String string `validate:"required"`
}
type B struct {
Int int64 `validate:"custom=A.String"`
}
type C struct {
A
B
}
func main() {
v := validator.New()
v.RegisterValidation("custom", customFunc)
c := &C{}
// Key: 'C.B.Int' Error:Field validation for 'Int' failed on the 'custom' tag
c.String = "hoge"
c.Int = 3
err := v.Struct(c)
fmt.Println(err)
// nil
c.String = "foo"
c.Int = 3
err = v.Struct(c)
fmt.Println(err)
}
func customFunc(fl validator.FieldLevel) bool {
param := fl.Param()
field, _, _, found := fl.GetStructFieldOKAdvanced2(fl.Top(), param)
return found && int(fl.Field().Int()) == len(field.String())
}