Open
Description
Package version eg. v8, v9:
github.com/go-playground/validator/v10 v10.2.0
github.com/go-playground/locales v0.13.0
github.com/go-playground/universal-translator v0.17.0
Issue, Question or Enhancement:
Trying to add validation with plurals in errors in ru_RU locale. Got nil pointer panic. What am I doing wrong?
Code sample, to showcase or reproduce:
This code leads to panic:
package main
import (
"github.com/go-playground/locales/ru_RU"
"github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
"github.com/go-playground/validator/v10/translations/ru"
"log"
)
type S struct {
Field string `validate:"gt=2"`
}
func main() {
locale := ru_RU.New()
uni := ut.New(locale, locale)
trans, _ := uni.GetTranslator(locale.Locale())
validate := validator.New()
_ = ru.RegisterDefaultTranslations(validate, trans)
a := &S{
Field: "a",
}
err := validate.Struct(a)
if err != nil {
for _, e := range err.(validator.ValidationErrors) {
log.Println(e.Translate(trans))
}
}
}
Panic:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x10c9cc2]
goroutine 1 [running]:
github.com/go-playground/universal-translator.(*translator).C(0xc0002440f0, 0x117da00, 0x11f04f0, 0x4000000000000000, 0x0, 0x12edcd2, 0x1, 0xc0002441e0, 0xc000252080, 0x1, ...)
/Users/al.gromov/go/pkg/mod/github.com/go-playground/[email protected]/translator.go:335 +0x112
github.com/go-playground/validator/v10/translations/ru.RegisterDefaultTranslations.func14(0x11f9c40, 0xc0002440f0, 0x11f6dc0, 0xc0000ca510, 0xc00024f708, 0x1190d01)
/Users/al.gromov/go/pkg/mod/github.com/go-playground/validator/[email protected]/translations/ru/ru.go:648 +0x88c
github.com/go-playground/validator/v10.(*fieldError).Translate(0xc0000ca510, 0x11f9c40, 0xc0002440f0, 0x1174dc0, 0xc0001bfb20)
/Users/al.gromov/go/pkg/mod/github.com/go-playground/validator/[email protected]/errors.go:271 +0xdc
main.main()
/Users/al.gromov/projects/youla-store/cmd/youla-store/main.go:30 +0x206
exit status 2
The same code in en_GB locale works fine:
package main
import (
"github.com/go-playground/locales/en_GB"
"github.com/go-playground/universal-translator"
"github.com/go-playground/validator/v10"
"github.com/go-playground/validator/v10/translations/en"
"log"
)
type S struct {
Field string `validate:"gt=2"`
}
func main() {
locale := en_GB.New()
uni := ut.New(locale, locale)
trans, _ := uni.GetTranslator(locale.Locale())
validate := validator.New()
_ = en.RegisterDefaultTranslations(validate, trans)
a := &S{
Field: "a",
}
err := validate.Struct(a)
if err != nil {
for _, e := range err.(validator.ValidationErrors) {
log.Println(e.Translate(trans))
}
}
}
Result:
2020/03/20 10:27:43 Field must be greater than 2 characters in length