Dive on a struct now panics. It didn't fail before
- [x] I have looked at the documentation here first?
- [x] I have looked at the examples provided that may showcase my question here?
Package version v10.15.5 :
Issue, Question or Enhancement:
Following code used to work fine in v10.14.1 at least. Now that we upgraded to v10.15.5, it panics because of the dive validator.
I do realize the dive doesn't do anything on a struct and shouldn't even be here in the first place. But before v10.15.5, it was simply ignored and raised no error whatsoever. It is kind of a breaking change.
Code sample, to showcase or reproduce:
package main
import (
"log"
"github.com/go-playground/validator/v10"
)
type SimpleStruct struct {
Struct S `validate:"dive"`
}
type S struct{}
func main() {
s := SimpleStruct{}
v := validator.New()
err := v.Struct(s)
log.Println(err)
}
now raises
panic: dive error! can't dive on a non slice or map
goroutine 1 [running]:
github.com/go-playground/validator/v10.(*validate).traverseField(0xc00043e000, {0x60f140, 0x7749e0}, {0x599a20?, 0x7749e0?, 0x110?}, {0x595fe0?, 0x7749e0?, 0x7f59a5ffdf98?}, {0xc0003da900, ...}, ...)
/home/qlebas/workspace/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator.go:317 +0x298c
github.com/go-playground/validator/v10.(*validate).validateStruct(0xc00043e000, {0x60f140, 0x7749e0}, {0x599a20?, 0x7749e0?, 0xc000390000?}, {0x599a20?, 0x7749e0?, 0x0?}, {0x6117a0, ...}, ...)
/home/qlebas/workspace/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator.go:77 +0x745
github.com/go-playground/validator/v10.(*Validate).StructCtx(0xc00043da40, {0x60f140, 0x7749e0}, {0x599a20?, 0x7749e0?})
/home/qlebas/workspace/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator_instance.go:393 +0x445
github.com/go-playground/validator/v10.(*Validate).Struct(...)
/home/qlebas/workspace/go/pkg/mod/github.com/go-playground/validator/[email protected]/validator_instance.go:366
main.main()
/home/qlebas/workspace/onestock-monorepo/go/src/gitea.onestock-retail.com/OSLIB/request-utils.git/main/main.go:19 +0x3c
exit status 2
I've seen this too upgrading from v10.15.1 to v10.15.2.
https://github.com/go-playground/validator/compare/v10.15.1...v10.15.2
Please see https://github.com/go-playground/validator/issues/1157
This was thoroughly discussed in https://github.com/go-playground/validator/issues/1142 originally.
The TL;DR is that struct validations were not running as expected prior to this change. This is a correction of behaviour so that now validations are being called on structs which also corrected some issues with the required_with* validations.
Special concessions were made for the required tag running to be opt-in because it is confusing when coming from a number of other languages to Go, but will become the default in the next version.
Again I'm sorry for any inconvenience that it's causing however the alternative is far worse, validations that were thought to be running which weren't which can cause undefined behaviour for applications relying on these validations to catch bad data. This is why this change was classified as a fix/correction rather than a breaking change.