jiujuan/gin-tutorial

08validate/main.go 里的自定义验证函数注册失败

Opened this issue · 0 comments

问题描述
08validate/main.go 里的自定义验证函数 bookabledate 没有注册成功,报错了,错误信息如下:

Undefined validation function 'bookabledate' on field 'CheckIn'
~/go/pkg/mod/github.com/go-playground/validator/v10@v10.4.2/cache.go:289 (0xac919f8)
        (*Validate).parseFieldTagsRecursive: panic(strings.TrimSpace(fmt.Sprintf(undefinedValidation, current.tag, fieldName)))
~/go/pkg/mod/github.com/go-playground/validator/v10@v10.4.2/cache.go:150 (0xac90d2b)
        (*Validate).extractStructCache: ctag, _ = v.parseFieldTagsRecursive(tag, fld.Name, "", false)
~/go/pkg/mod/github.com/go-playground/validator/v10@v10.4.2/validator.go:37 (0xac954db)
        (*validate).validateStruct: cs = v.v.extractStructCache(current, typ.Name())
~/go/pkg/mod/github.com/go-playground/validator/v10@v10.4.2/validator_instance.go:313 (0xac9ad84)
        (*Validate).StructCtx: vd.validateStruct(ctx, top, val, val.Type(), vd.ns[0:0], vd.actualNs[0:0], nil)
~/go/pkg/mod/github.com/go-playground/validator/v10@v10.4.2/validator_instance.go:286 (0xae27204)
        (*Validate).Struct: return v.StructCtx(context.Background(), s)
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/binding/default_validator.go:30 (0xae271db)
        (*defaultValidator).ValidateStruct: if err := v.validate.Struct(obj); err != nil {
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/binding/binding.go:115 (0xae2af79)
        validate: return Validator.ValidateStruct(obj)
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/binding/query.go:20 (0xae2af61)
        queryBinding.Bind: return validate(obj)
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:679 (0xae46c84)
        (*Context).ShouldBindWith: return b.Bind(c.Request, obj)
~/github/gin-tutorial/08validate/main.go:58 (0xae46c85)
        getBookable: if err := c.ShouldBindWith(&b, binding.Query); err == nil {
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0xae3dc93)
        (*Context).Next: c.handlers[c.index](c)
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/recovery.go:83 (0xae3dc81)
        RecoveryWithWriter.func1: c.Next()
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0xae3cedc)
        (*Context).Next: c.handlers[c.index](c)
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/logger.go:241 (0xae3cec3)
        LoggerWithConfig.func1: c.Next()
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/context.go:161 (0xae3c428)
        (*Context).Next: c.handlers[c.index](c)
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:409 (0xae3c08d)
        (*Engine).handleHTTPRequest: c.Next()
~/go/pkg/mod/github.com/gin-gonic/gin@v1.6.3/gin.go:367 (0xae3bbc8)
        (*Engine).ServeHTTP: engine.handleHTTPRequest(c)
/usr/local/Cellar/go/1.22.4/libexec/src/net/http/server.go:3137 (0xac2efad)
        serverHandler.ServeHTTP: handler.ServeHTTP(rw, req)
/usr/local/Cellar/go/1.22.4/libexec/src/net/http/server.go:2039 (0xac2a367)
        (*conn).serve: serverHandler{c.server}.ServeHTTP(w, w.req)
/usr/local/Cellar/go/1.22.4/libexec/src/runtime/asm_amd64.s:1695 (0xa9e0040)
        goexit: BYTE    $0x90   // NOP

把 gopkg.in/go-playground/validator.v8 换成另外一个 github.com/go-playground/validator/v10 包进行导入,然后验证函数的定义改成下面这样的,就正常了

func bookableDate(fl validator.FieldLevel) bool {
	if date, ok := fl.Field().Interface().(time.Time); ok {
		today := time.Now()
		if today.Year() > date.Year() || (today.Year() == date.Year() && today.YearDay() > date.YearDay()) {
			return false
		}
	}
	return true
}