Skip to content

RequiredIfValidation does not support pointers #334

@manicar2093

Description

@manicar2093

System (please complete the following information):

  • OS: macOS [e.g. linux, macOS]
  • GO Version: 1.25.0 [e.g. 1.13]
  • Pkg Version: 1.5.7 [e.g. 1.1.1]

Describe the bug

requiredIf validator does not support pointer values

To Reproduce

package main

import (
	"fmt"

	"github.com/gookit/validate"
)

type Person struct {
	IsOld      *bool
	Experience *string `validate:"requiredIf:IsOld,true"`
}

func main() {
	t := true
	e := ""
	v1 := Person{
		IsOld:      &t,
		Experience: &e,
	}
	val := validate.Struct(&v1)

	val.Validate()

	fmt.Println(val.IsSuccess())
	fmt.Println(v1.Experience)

	v2 := Person{
		IsOld: &t,
	}
	val = validate.Struct(&v2)

	val.Validate()

	fmt.Println(val.IsSuccess())
	fmt.Println(v2.Experience)

}

Expected behavior

Since for v1 Experience is empty should return false on IsSuccess() call. In case of v2 Experience is nil and IsSuccess() should return false too.

Screenshots

Image

Additional context

Giving it a check I found the problem is in goutil@v0.7.4/reflects/conv.go::L91 on ConvToKind() function. This is called by Validation method RequiredIf. Since does ConvToKind() function does not support pointers by default returns a true validation

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions