go-playground / validator

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

Undefined validation function 'required_if' #1059

Closed rozanecm closed 1 year ago

rozanecm commented 1 year ago

Package version eg. v9, v10:

v10

Issue, Question or Enhancement:

When trying to use the required_if validator, the app panics:

panic: Undefined validation function 'required_if:Type type_1' on field 'ResourceID'

When looking for a solution, the most common suggestion is to check which version is being used. This was introduced in v10.4, so using any older version could cause this panic. It's not the case here, though, as can be seen in the logs below where version is 10.11.

Tanks in advance for any help!

Code sample, to showcase or reproduce:

This snippet can also be found (in the playground)[https://go.dev/play/p/OfydNe1Gv-g]

package main

import (
    "fmt"

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

type t struct {
    Type       string `json:"type"`
    ResourceID string `json:"resource_id,omitempty" validate:"required_if:Type type_1"`
}

func main() {
    v := t{
        Type: "type_1",
    }

    validate := validator.New()
    err := validate.Struct(v)
    fmt.Println(err)

}

Trace:

panic: Undefined validation function 'required_if:Type type_1' on field 'ResourceID'

goroutine 1 [running]:
github.com/go-playground/validator/v10.(*Validate).parseFieldTagsRecursive(0xc00043acb0, {0x54957c?, 0x573bf0?}, {0x54954a, 0xa}, {0x0, 0x0}, 0x0)
    /tmp/gopath3631371347/pkg/mod/github.com/go-playground/validator/v10@v10.11.1/cache.go:294 +0xa19
github.com/go-playground/validator/v10.(*Validate).extractStructCache(0xc00043acb0, {0x553ea0?, 0xc0004117e0?, 0x0?}, {0x53850d, 0x1})
    /tmp/gopath3631371347/pkg/mod/github.com/go-playground/validator/v10@v10.11.1/cache.go:155 +0x60f
github.com/go-playground/validator/v10.(*validate).validateStruct(0xc00007dc20, {0x5ac320, 0xc00001c030}, {0x553ea0?, 0xc0004117e0?, 0x20?}, {0x553ea0?, 0xc0004117e0?, 0xa?}, {0x5ae2e0, ...}, ...)
    /tmp/gopath3631371347/pkg/mod/github.com/go-playground/validator/v10@v10.11.1/validator.go:37 +0x190
github.com/go-playground/validator/v10.(*Validate).StructCtx(0xc00043acb0, {0x5ac320, 0xc00001c030}, {0x553ea0, 0xc0004117e0?})
    /tmp/gopath3631371347/pkg/mod/github.com/go-playground/validator/v10@v10.11.1/validator_instance.go:385 +0x46c
github.com/go-playground/validator/v10.(*Validate).Struct(...)
    /tmp/gopath3631371347/pkg/mod/github.com/go-playground/validator/v10@v10.11.1/validator_instance.go:358
main.main()
    /tmp/sandbox522856025/prog.go:20 +0x70

Program exited.
vuon9 commented 1 year ago

Not required_if: but required_if=

rozanecm commented 1 year ago

Can't believe this; thank you!! Much appreciated!

vijaynallagatla commented 1 year ago

@rozanecm It'd be good to close this issue. notion of having too many issues would be definitely misleading the choice of opting this lib :)