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

filter 无法对切片中的元素应用 #262

Open
devhaozi opened this issue Mar 16, 2024 · 1 comment
Open

filter 无法对切片中的元素应用 #262

devhaozi opened this issue Mar 16, 2024 · 1 comment
Assignees
Labels
enhancement New feature or request

Comments

@devhaozi
Copy link

System (please complete the following information):

  • OS: linux [e.g. linux, macOS]
  • GO Version: 1.22 [e.g. 1.13]
  • Pkg Version: 1.5.2 [e.g. 1.1.1]

Describe the bug

filter 无法对切片中的元素应用,报错:
_filter: ports.*.container_start: convert value type error

To Reproduce

package main

import (
	"fmt"

	"github.com/gookit/validate"
)

type ContainerPort struct {
	ContainerStart int    `json:"container_start"`
	ContainerEnd   int    `json:"container_end"`
	Protocol       string `json:"protocol"`
}

type ContainerCreate struct {
	Ports []ContainerPort `json:"ports"`
}

func main() {
	data := `{"ports":[{"container_start":80,"container_end":80,"host_start":88,"host_end":88,"host":"","protocol":"tcp"}]}`
	v, _ := validate.FromJSON(data)
	fmt.Println(v.Src())
	v2 := v.Create()
	v2.FilterRule("ports.*.container_start", "int")
	v2.FilterRule("ports.*.container_end", "int")
	v2.AddRule("ports.*.container_start", "int")
	v2.AddRule("ports.*.container_end", "int")
	v2.AddRule("ports.*.protocol", "string")

	if v2.Validate() {
		fmt.Println(v2.SafeData())
	} else {
		fmt.Println(v2.Errors)
	}
}

Expected behavior

正确将切片中的子元素转为 int 类型。

Screenshots

图片

Additional context

Add any other context about the problem here.

@devhaozi
Copy link
Author

devhaozi commented Mar 16, 2024

如果类型改成 float64, 并且注释掉 filter 规则,可以正常通过验证:

package main

import (
	"fmt"

	"github.com/gookit/validate"
)

type ContainerPort struct {
	ContainerStart float64 `json:"container_start"`
	ContainerEnd   float64 `json:"container_end"`
	Protocol       string  `json:"protocol"`
}

type ContainerCreate struct {
	Ports []ContainerPort `json:"ports"`
}

func main() {
	data := `{"ports":[{"container_start":80,"container_end":80,"host_start":88,"host_end":88,"host":"","protocol":"tcp"}]}`
	v, _ := validate.FromJSON(data)
	fmt.Println(v.Src())
	v2 := v.Create()
	/*v2.FilterRule("ports.*.container_start", "int")
	v2.FilterRule("ports.*.container_end", "int")*/
	v2.AddRule("ports.*.container_start", "float")
	v2.AddRule("ports.*.container_end", "float")
	v2.AddRule("ports.*.protocol", "string")

	if v2.Validate() {
		fmt.Println(v2.SafeData())
	} else {
		fmt.Println(v2.Errors)
	}
}

@inhere inhere added the enhancement New feature or request label Mar 17, 2024
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