go-playground / validator

:100:Go Struct and Field validation, including Cross Field, Cross Struct, Map, Slice and Array diving
MIT License
16.88k stars 1.32k forks source link

excluded_if panic on nil tag parameter #1320

Open DarkReduX opened 2 months ago

DarkReduX commented 2 months ago

Package version eg. v9, v10:

V10

Issue, Question or Enhancement:

tag excluded_if app panics on nil field value in tag param

Code sample, to showcase or reproduce:


type Testr struct {
    W *int
    R *int `validate:"excluded_if=W nil,max=10"`
}

func TestN(t *testing.T) {
    v := validator.NewMapValidator()

    var (
        r = 0
        w = 10
    )

    err := v.Struct(Testr{
        W: &w,
        R: &r,
    })
    t.Fatalf("%s", err)

}

Output

panic: strconv.ParseInt: parsing "nil": invalid syntax [recovered] panic: strconv.ParseInt: parsing "nil": invalid syntax

goroutine 21 [running]: testing.tRunner.func1.2({0x10335e0, 0xc0000d6f20}) C:/Program Files/Go/src/testing/testing.go:1545 +0x238 testing.tRunner.func1() C:/Program Files/Go/src/testing/testing.go:1548 +0x397 panic({0x10335e0?, 0xc0000d6f20?}) C:/Program Files/Go/src/runtime/panic.go:914 +0x21f github.com/go-playground/validator/v10.panicIf(...) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/util.go:294 github.com/go-playground/validator/v10.asInt({0x102bb2a?, 0x104c5c0?}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/util.go:229 +0x48 github.com/go-playground/validator/v10.requireCheckFieldValue({0x10d62b8, 0xc0000bcb40}, {0x102bb28, 0x1}, {0x102bb2a, 0x3}, 0xf?) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/baked_in.go:1816 +0x167 github.com/go-playground/validator/v10.excludedIf({0x10d62b8, 0xc0000bcb40}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/baked_in.go:1869 +0xac github.com/go-playground/validator/v10.New.wrapFunc.func2({0xc0000bcb40?, 0x102d580?}, {0x10d62b8?, 0xc0000bcb40?}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/baked_in.go:43 +0x27 github.com/go-playground/validator/v10.(validate).traverseField(0xc0000bcb40, {0x10d50a8, 0x12a0b00}, {0x104c5c0?, 0xc0000d6ea0?, 0x110?}, {0x102d580?, 0xc0000d6ea8?, 0x196ecb71268?}, {0xc0000a21c0, ...}, ...) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator.go:456 +0x10ce github.com/go-playground/validator/v10.(validate).validateStruct(0xc0000bcb40, {0x10d50a8, 0x12a0b00}, {0x104c5c0?, 0xc0000d6ea0?, 0xc000049eb8?}, {0x104c5c0?, 0xc0000d6ea0?, 0x10?}, {0x10d7ae0, ...}, ...) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator.go:78 +0x745 github.com/go-playground/validator/v10.(Validate).StructCtx(0xc0000ba540, {0x10d50a8, 0x12a0b00}, {0x104c5c0?, 0xc0000d6ea0?}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator_instance.go:395 +0x445 github.com/go-playground/validator/v10.(Validate).Struct(...) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator_instance.go:368 git.fg13.net/roolz/roolz-pkg-core/validator.(MapRulesValidator).Struct(...) C:/Users/Kirill/GolandProjects/roolz-pkg-core/validator/validator_benchmark.go:134 git.fg13.net/roolz/roolz-pkg-core/validator_test.TestN(0xc000084680) C:/Users/Kirill/GolandProjects/roolz-pkg-core/validator/validator_test.go:165 +0x93 testing.tRunner(0xc000084680, 0x1095600) C:/Program Files/Go/src/testing/testing.go:1595 +0xff created by testing.(T).Run in goroutine 1 C:/Program Files/Go/src/testing/testing.go:1648 +0x3ad

DarkReduX commented 2 months ago

on Structure fields values {nil, nil} panics too

panic: Bad field type int [recovered] panic: Bad field type int

goroutine 10 [running]: testing.tRunner.func1.2({0x9635e0, 0xc0001c94a0}) C:/Program Files/Go/src/testing/testing.go:1545 +0x238 testing.tRunner.func1() C:/Program Files/Go/src/testing/testing.go:1548 +0x397 panic({0x9635e0?, 0xc0001c94a0?}) C:/Program Files/Go/src/runtime/panic.go:914 +0x21f github.com/go-playground/validator/v10.isLte({0xa062b8, 0xc000135b00}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/baked_in.go:2389 +0x4cc github.com/go-playground/validator/v10.hasMaxOf({0xa062b8?, 0xc000135b00?}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/baked_in.go:2442 +0x1d github.com/go-playground/validator/v10.New.wrapFunc.func3({0xc000135b00?, 0xc000019bc0?}, {0xa062b8?, 0xc000135b00?}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/baked_in.go:43 +0x27 github.com/go-playground/validator/v10.(validate).traverseField(0xc000135b00, {0xa050a8, 0xbd0b00}, {0x97c5c0?, 0xc0001c9420?, 0x110?}, {0x95d580?, 0xc0001c9428?, 0x1c9bfc7b838?}, {0xc000019bc0, ...}, ...) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator.go:456 +0x10ce github.com/go-playground/validator/v10.(validate).validateStruct(0xc000135b00, {0xa050a8, 0xbd0b00}, {0x97c5c0?, 0xc0001c9420?, 0xc00004fec0?}, {0x97c5c0?, 0xc0001c9420?, 0x10?}, {0xa07ae0, ...}, ...) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator.go:78 +0x745 github.com/go-playground/validator/v10.(Validate).StructCtx(0xc000118770, {0xa050a8, 0xbd0b00}, {0x97c5c0?, 0xc0001c9420?}) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator_instance.go:395 +0x445 github.com/go-playground/validator/v10.(Validate).Struct(...) C:/Users/Kirill/GolandProjects/pkg/mod/github.com/go-playground/validator/v10@v10.22.1/validator_instance.go:368 git.fg13.net/roolz/roolz-pkg-core/validator.(MapRulesValidator).Struct(...) C:/Users/Kirill/GolandProjects/roolz-pkg-core/validator/validator_benchmark.go:134 git.fg13.net/roolz/roolz-pkg-core/validator_test.TestN(0xc0001c61a0) C:/Users/Kirill/GolandProjects/roolz-pkg-core/validator/validator_test.go:165 +0x72 testing.tRunner(0xc0001c61a0, 0x9c5600) C:/Program Files/Go/src/testing/testing.go:1595 +0xff created by testing.(T).Run in goroutine 1 C:/Program Files/Go/src/testing/testing.go:1648 +0x3ad

DarkReduX commented 2 months ago

add check if is param values is nil and field value is pointer than compare