Skip to content

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

@devhaozi

Description

@devhaozi

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.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions