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

Panic on max, min Validation Tag with struct Fields in Version v10.20.0 and above #1300

Open
2 tasks done
iamsushank opened this issue Aug 2, 2024 · 4 comments
Open
2 tasks done

Comments

@iamsushank
Copy link

iamsushank commented Aug 2, 2024

  • 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.20.0

Issue, Question or Enhancement:

The go-playground/validator package panics when validating struct fields with the max tag on null.Int fields. This issue did not occur in earlier versions. Previously, the package would not panic.

Code sample, to showcase or reproduce:

package main

import (
	"fmt"
	"github.com/go-playground/validator/v10"
	"gopkg.in/guregu/null.v4"
)

type User struct {
	Name     string      `json:"name"`
	Age      int         `json:"age"`
	NullAge  null.Int    `validate:"max=10"`
}

func main() {
	v := validator.New()
	user := User{
		Name:     "John Doe",
		Age:      30,
		NullAge:  null.IntFrom(15),
	}

	err := v.Struct(user)
	if err != nil {
		fmt.Println("Validation failed:", err)
	} else {
		fmt.Println("Validation succeeded")
	}
}

Expected behavior:

The validator should not panic when encountering the max tag on null.Int fields. Ideally, it should either validate the null.Int field properly or skip validation without causing a panic.

Actual behavior:

The validator panics when it encounters the max tag on null.Int fields.

Steps to reproduce:

1.	Define a struct with a null.Int field and a max validation tag.
2.	Create an instance of the struct with a value for the null.Int field.
3.	Run the validator on the struct.

There might me other tags aswell which now panics with struct i have checked with min, max only

@iamsushank iamsushank changed the title The go-playground/validator package panics when validating struct fields with the max tag on null.Int fields. This issue did not occur in earlier versions. Previously, the package would not panic. Panic on max Validation Tag with null.Int (struct) Fields in Version v10.20.0 Aug 2, 2024
@iamsushank iamsushank changed the title Panic on max Validation Tag with null.Int (struct) Fields in Version v10.20.0 Panic on max, min Validation Tag with null.Int (struct) Fields in Version v10.20.0 Aug 2, 2024
@iamsushank iamsushank changed the title Panic on max, min Validation Tag with null.Int (struct) Fields in Version v10.20.0 Panic on max, min Validation Tag with null.Int (struct) Fields in Version v10.20.0 and above Aug 2, 2024
@iamsushank iamsushank changed the title Panic on max, min Validation Tag with null.Int (struct) Fields in Version v10.20.0 and above Panic on max, min Validation Tag with struct Fields in Version v10.20.0 and above Aug 2, 2024
@vasi1e
Copy link

vasi1e commented Aug 19, 2024

I also have the same problem starting from version 10.16.0 but with my own custom struct

@slugbyte
Copy link

I have the same issue for null.String with min and max

@remvn
Copy link

remvn commented Sep 24, 2024

You need to register a custom func to pull out the "real" value of custom struct:

func makeValidator() *validator.Validate {
	validate := validator.New(validator.WithRequiredStructEnabled())

	// register func for custom struct, do this for every custom struct
	// you're going to need
	validate.RegisterCustomTypeFunc(validateValuer, sql.NullString{}, sql.NullInt64{})
	return validate
}

func validateValuer(field reflect.Value) interface{} {
	if valuer, ok := field.Interface().(driver.Valuer); ok {
		val, err := valuer.Value()
		if err == nil {
			// return the "real" value of custom struct
			// for example: if concrete type of driver.Valuer interface
			// is sql.NullString then val will be a string
			return val
		}
	}
	// return nil means this field is indeed "null".
	// field with tag `required` will fail the check
	return nil
}

Check out my article here for a detail explanation, why it panics when you add min, max tag.

@vasi1e
Copy link

vasi1e commented Oct 21, 2024

I also have the same problem starting from version 10.16.0 but with my own custom struct

Actually after some more testing and investigation it turned out my problem was not because of nullable value. I had slice of custom struct and using Gin I also said the during the binding to "dive" and validate my custom struct beside checking the maximum of elements in the slice.

type ToValidate struct {
    S []CustomStruct  'json:"s" binding:"dive,max=50"'
}

So in the end I just needed to change the order of the binding rules to "max=50,dive" and this fixed my issue! Not sure why the messed order was working for me in older versions.

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

4 participants