Skip to content

[Bug]: validator panic with []byte and required_if #1437

Open
@qiuchengxuan

Description

@qiuchengxuan

What happened?

Program panic when using []byte with required_if

panic: strconv.ParseInt: parsing "nil": invalid syntax

goroutine 1 [running]:
github.com/go-playground/validator/v10.panicIf(...)
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/util.go:294
github.com/go-playground/validator/v10.asInt({0x5b6e9d?, 0x5d6380?})
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/util.go:229 +0x48
github.com/go-playground/validator/v10.requireCheckFieldValue({0x6551b0, 0xc00016a7e0}, {0x5b6e97, 0x5}, {0x5b6e9d, 0x3}, 0x0)
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/baked_in.go:1868 +0x3b1
github.com/go-playground/validator/v10.requiredIf({0x6551b0, 0xc00016a7e0})
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/baked_in.go:1893 +0xac
github.com/go-playground/validator/v10.New.wrapFunc.func2({0xc00016a7e0?, 0x5bedc0?}, {0x6551b0?, 0xc00016a7e0?})
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/baked_in.go:44 +0x27
github.com/go-playground/validator/v10.(*validate).traverseField(0xc00016a7e0, {0x654030, 0x7d6ea0}, {0x5d6380?, 0xc0001cc0c0?, 0xc0000a2d50?}, {0x5bedc0?, 0xc0001cc0c0?, 0x0?}, {0xc000182280, ...}, ...)
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/validator.go:473 +0x11e3
github.com/go-playground/validator/v10.(*validate).validateStruct(0xc00016a7e0, {0x654030, 0x7d6ea0}, {0x5b7f40?, 0xc0001cc0c0?, 0x1306a8?}, {0x5d6380?, 0xc0001cc0c0?, 0xc0001cc090?}, {0x656cd8, ...}, ...)
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/validator.go:78 +0x725
github.com/go-playground/validator/v10.(*Validate).StructCtx(0xc000198150, {0x654030, 0x7d6ea0}, {0x5b7f40, 0xc0001cc0c0})
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/validator_instance.go:396 +0x445
github.com/go-playground/validator/v10.(*Validate).Struct(...)
        /home/qiubuyi/.go/pkg/mod/github.com/go-playground/validator/v10@v10.26.0/validator_instance.go:369
main.main()
        /home/qiubuyi/test/main.go:18 +0x86
exit status 2

Version

v10.26.0, v10.25.0 also affected

Example Code

package main

import (
        "github.com/go-playground/validator/v10"
)

type TextOrBytes struct {
        Text string `validate:"required_if=Bytes nil"`
        Bytes []byte `validate:"required_if=Text nil"`
}

var validate *validator.Validate

func main() {
        validate = validator.New(validator.WithRequiredStructEnabled())
        validate.Struct(&TextOrBytes{})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions