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

FieldLevel.Field().Type() takes a dynamic value when a field is of a pointer type. #1203

Open
2 tasks done
your-diary opened this issue Dec 13, 2023 · 0 comments
Open
2 tasks done

Comments

@your-diary
Copy link

  • 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 a custom tag defined with RegisterValidation() is added to a field of a pointer type, the FieldLevel passed to the registered function behaves very strangely.

For example, if a validated field is of the type *string, FieldLevel.Field().Type() returns *string if the actual value is nil while it returns string if the actual value is not nil.

I expect it always returns *string because the original field is of the type *string (not string).

Maybe the package auto-dereferences validated fields? But the official documentation says nothing about it.

Code sample, to showcase or reproduce:

package main

import (
	"fmt"

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

type S struct {
	Name *string `validate:"my"`
}

func f(s validator.FieldLevel) bool {
	fmt.Println(s.Field().Type())
	return true
}

func main() {
	validate := validator.New()
	err := validate.RegisterValidation("my", f)
	if err != nil {
		panic(err)
	}

	{
		s := S{
			Name: nil,
		}
		_ = validate.Struct(s) //=> *string
	}

	{
		ss := "black"
		s := S{
			Name: &ss,
		}
		_ = validate.Struct(s) //=> string
	}
}
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

1 participant