Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefined validation function 'required_if' #1059

Closed
2 tasks done
rozanecm opened this issue Jan 25, 2023 · 3 comments
Closed
2 tasks done

Undefined validation function 'required_if' #1059

rozanecm opened this issue Jan 25, 2023 · 3 comments

Comments

@rozanecm
Copy link

rozanecm commented Jan 25, 2023

  • 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

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
Copy link
Contributor

vuon9 commented Jan 27, 2023

Not required_if: but required_if=

@rozanecm
Copy link
Author

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

@vijaynallagatla
Copy link
Contributor

@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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants