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

Pointer of UUID will break the Validate() function #247

Closed
jeremy-temelpa opened this issue Nov 18, 2023 · 5 comments
Closed

Pointer of UUID will break the Validate() function #247

jeremy-temelpa opened this issue Nov 18, 2023 · 5 comments
Assignees
Labels
bug_fixed bug Something isn't working

Comments

@jeremy-temelpa
Copy link

jeremy-temelpa commented Nov 18, 2023

System (please complete the following information):

  • OS: macOS
  • GO Version: 1.20
  • Pkg Version: 1.5.1

Describe the bug

When the struct under validation contains a pointer of UUID, the Validate function will fail. With error msg

panic: reflect: call of reflect.Value.Len on zero Value [recovered]
	panic: reflect: call of reflect.Value.Len on zero Value

To Reproduce

// go code
type TestUser struct {
	Name             string  `validate:"required|minLen:2|maxLen:100" filter:"trim|lower"`
	Email            string  `validate:"required|email" filter:"lower"`
	OptionalString   *string `validate:"required|minLen:2|maxLen:100" filter:"trimPtr|lowerPtr"`
	TestOptionalUUID *uuid.UUID // **Problem LINE**
}

func TestValidateStruct(t *testing.T) {
	t.Parallel()
	validation.RegisterValidationFilters()
	user := TestUser{
		Name:           "  John Doe  ",
		Email:          "JOHNDOE@EXAMPLE.COM",
		OptionalString: pointer.Ref("    Optional String   "),
	}

	err := validation.ValidateStruct(&user)

	// Check if there's an error and it's as expected
	if err != nil {
		t.Errorf("Validation failed: %v", err)
	}

	// Check if the filters were applied correctly
	if user.Name != "john doe" || user.Email != "johndoe@example.com" {
		t.Errorf("Filters not applied correctly. Got Name: %s, Email: %s", user.Name, user.Email)
	}

	if pointer.Deref(user.OptionalString) != "optional string" {
		t.Errorf("Filters not applied correctly. Got OptionalString: %s", *user.OptionalString)
	}
}

func trimPointerString(val interface{}) interface{} {
	if strPtr, ok := val.(*string); ok && strPtr != nil {
		trimmed := strings.TrimSpace(*strPtr)
		return &trimmed
	}
	return val
}

func lowerPointerString(val interface{}) interface{} {
	if strPtr, ok := val.(*string); ok && strPtr != nil {
		lowered := strings.ToLower(*strPtr)
		return &lowered
	}
	return val
}

func RegisterValidationFilters() {
	validate.AddFilter("trimPtr", trimPointerString)
	validate.AddFilter("lowerPtr", lowerPointerString)
}

func ValidateStruct[T any](input *T) error {
	if input == nil {
		return nil
	}
	v := validate.Struct(input)
	if v.Validate() {
		return nil
	} else {
		return errorx.New(v.Errors.Error())
	}
}

Expected behavior

Validate() should works fine without error

Screenshots

image
@inhere inhere added the bug Something isn't working label Dec 13, 2023
@manicar2093
Copy link

Hi!. I was checking this and yes, this kind of validation does not work. I think this is due the nature of uuid google's package. Could this happend with other types based on Bytes?

@ip75
Copy link

ip75 commented Dec 26, 2023

yes. Validation falls to panic in the recursive function. https://go.dev/play/p/sfexq-rEVK9

@ip75
Copy link

ip75 commented Dec 26, 2023

Hi!. I was checking this and yes, this kind of validation does not work. I think this is due the nature of uuid Google's package. Could this append with other types based on Bytes?

https://go.dev/play/p/fTVTY9roF3s - this test with github.com/gofrs/uuid package but result is the same. Bug is in validation code.

https://go.dev/play/p/FhuzBTFpjX_c - pointer to any array (fixed array or byte slice) throws this panic. You can remove size of array to get slice and check.

func parseRulesFromTag while running func recursiveFunc

@inhere
Copy link
Member

inhere commented Jan 5, 2024

hi @ip75 @manicar2093 @jeremy-temelpa The problem has been fixed, please wait for the next version to be released.

@inhere
Copy link
Member

inhere commented Jan 24, 2024

Please upgrade to https://github.com/gookit/validate/releases/tag/v1.5.2

@inhere inhere closed this as completed Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug_fixed bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants