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

Error when using custom filter with pointer to slice of strings #255

Closed
kaptinlin opened this issue Jan 9, 2024 · 2 comments
Closed

Error when using custom filter with pointer to slice of strings #255

kaptinlin opened this issue Jan 9, 2024 · 2 comments
Assignees
Labels
bug_fixed bug Something isn't working

Comments

@kaptinlin
Copy link
Contributor

kaptinlin commented Jan 9, 2024

System (please complete the following information):

  • OS: linux
  • GO Version: 1.21.0
  • Pkg Version: the master latest

Describe the bug

When using a custom filter ptrTrimStrings to trim whitespace from each string in a pointer to a slice of strings, the validation fails with a type conversion error.

To Reproduce

package main

import (
	"strings"
	"github.com/gookit/validate"
)

func main() {
	validate.AddFilters(validate.M{
		"ptrTrimStrings": func(val *[]string) *[]string {
			trimmedSlice := make([]string, len(*val))
			for i, str := range *val {
				trimmedSlice[i] = strings.TrimSpace(str)
			}
			return &trimmedSlice
		},
	})

	type Request struct {
		PtrStringSlice *[]string `json:"string_slice" filter:"ptrTrimStrings" validate:"strings"`
	}
	stringSlice := []string{" foobar "}
	v := validate.New(&Request{
		PtrStringSlice: &stringSlice,
	})

	if err := v.Validate(); err != nil {
		panic(err)
	}
}

Expected behavior

The custom filter ptrTrimStrings should trim the whitespace from each string in the slice without any errors, and the validation should pass successfully.

Screenshots

N/A

Additional context

The error message received is:

"_filter": {
    "_filter": "PtrStringSlice: convert value type error"
}

This suggests that there might be an issue with the filter function's return type or the way the library handles the conversion of filtered values for pointer to slice fields.

@inhere inhere added the bug Something isn't working label Jan 9, 2024
@inhere
Copy link
Member

inhere commented Jan 23, 2024

hi @kaptinlin fixed in the latest codes.

@inhere inhere closed this as completed Jan 24, 2024
@inhere
Copy link
Member

inhere commented 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

2 participants