Open
Description
- I have looked at the documentation here first?
- I have looked at the examples provided that may showcase my question here?
Package version eg. v9, v10:
v10.10.0
Issue, Question or Enhancement:
When validating an int64 pointer with required_if
and omitempty
at the same time, calling Struct()
leads to panic.
Using omitempty
merely is ok. Validating an int64 instead of an int64 pointer is ok.
Similar to: Panic Bad field type *string, when using required_without and omitempty at the same time #654
Code sample, to showcase or reproduce:
package main
import (
"github.com/go-playground/validator/v10"
"log"
)
type Sample struct {
Type string `json:"type" validate:"required"`
Quantity int64 `json:"quantity" validate:"required_if=Type special,gte=2"`
}
type SampleWithPointer struct {
Type string `json:"type" validate:"required"`
Quantity *int64 `json:"quantity,omitempty" validate:"required_if=Type special,gte=2"`
}
func main() {
_type := "notspecial"
s := &Sample{Type: _type}
swp := &SampleWithPointer{Type: _type}
validate := validator.New()
if err := validate.Struct(s); err != nil {
log.Printf("Validate sample with error, %v", err)
}
if err := validate.Struct(swp); err != nil {
log.Printf("Validate SampeWithPointer with error, %v", err)
}
}
stack trace:
2009/11/10 23:00:00 Validate sample with error, Key: 'Sample.Quantity' Error:Field validation for 'Quantity' failed on the 'gte' tag
panic: Bad field type *int64
goroutine 1 [running]:
github.com/go-playground/validator/v10.isGte({0x59b798, 0xc0000d1c20})
/tmp/gopath931800745/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/baked_in.go:1789 +0x4dd
github.com/go-playground/validator/v10.wrapFunc.func1({0x534940, 0xc0000ac208}, {0x59b798, 0xc0000d1c20})
/tmp/gopath931800745/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/baked_in.go:40 +0x2d
github.com/go-playground/validator/v10.(*validate).traverseField(0xc0000d1c20, {0x599a70, 0xc0000ba000}, {0x5471a0, 0xc0000ac1f8, 0x13}, {0x534940, 0xc0000ac208, 0x85}, {0xc000350300, ...}, ...)
/tmp/gopath931800745/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/validator.go:445 +0x16a7
github.com/go-playground/validator/v10.(*validate).validateStruct(0xc0000d1c20, {0x599a70, 0xc0000ba000}, {0x534e00, 0xc0000ac1f8, 0x1}, {0x5471a0, 0xc0000ac1f8, 0xc0000a31e0}, {0x59bd60, ...}, ...)
/tmp/gopath931800745/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/validator.go:77 +0x765
github.com/go-playground/validator/v10.(*Validate).StructCtx(0xc0003492c0, {0x599a70, 0xc0000ba000}, {0x534e00, 0xc0000ac1f8})
/tmp/gopath931800745/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/validator_instance.go:344 +0x430
github.com/go-playground/validator/v10.(*Validate).Struct(...)
/tmp/gopath931800745/pkg/mod/github.com/go-playground/validator/v10@v10.10.0/validator_instance.go:317
main.main()
/tmp/sandbox1464673145/prog.go:29 +0x13d
Program exited.