8000 Feature request: Allow filter rules on slice elements · Issue #172 · gookit/validate · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Feature request: Allow filter rules on slice elements #172

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

Closed
OscarVanL opened this issue Sep 30, 2022 · 1 comment
Closed

Feature request: Allow filter rules on slice elements #172

OscarVanL opened this issue Sep 30, 2022 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@OscarVanL
Copy link
Contributor
OscarVanL commented Sep 30, 2022

System (please complete the following information):

  • OS: linux
  • GO Version: 1.19
  • Pkg Version: 1.4.4

Describe the bug

I would like to apply the string trim filter to each element in a slice.

For example with input {" hello ", " test", "test2 "} I would like output {"hello", "test", "test2"}.

To Reproduce

I have made a Go Playground where I tried using the existing StringRule, but this didn't work.
https://go.dev/play/p/4GiKMauL0hn

// You can edit this code!
// Click here and start typing.
package main

import (
	"log"

	"github.com/gookit/validate"
)

type Foo struct {
	Domains []string
}

func (gs Foo) ConfigValidation(v *validate.Validation) {
	v.StringRule("Domains.*", "trim")
}

func main() {
	f := Foo{Domains: []string{"   test.com   ", "oof.com", " foobar.com"}}
	v := validate.Struct(&f) // nolint:varnamelen
	if !v.Validate() {
		log.Println(v.Errors)
	}
	log.Println(f)
}

Output is: panic: validate: the validator 'trim' does not exist

Expected behavior

I would expect the whitespace to be trimmed and the output to look like: Output is: {[test.com oof.com foobar.com]}

@OscarVanL OscarVanL changed the title Filter rules cannot be defined on Slices Feature request: Allow filter rules on slice elements Sep 30, 2022
@inhere inhere added the enhancement New feature or request label Oct 1, 2022
@inhere inhere closed this as completed in 087269d Oct 2, 2022
@inhere inhere reopened this Oct 2, 2022
@OscarVanL
Copy link
Contributor Author

Thanks, the trimStrings filter achieved what I wanted! Thanks for the test case in the MR which showed how to achieve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants
0