gookit / validate

⚔ Go package for data validation and filtering. support Map, Struct, Form data. Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器,支持自定义验证器、自定义消息、字段翻译。

Home Page:https://gookit.github.io/validate/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

POST 请求中,一个 key 包含多个上传文件时,除了第一个文件,其他文件被丢弃,导致 `BindSafeData` 行为非预期。

hwbrzzl opened this issue · comments

System (please complete the following information):

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

Describe the bug

POST 请求中,一个 key 包含多个上传文件时,除了第一个文件,其他文件被丢弃。导致 BindSafeData 只能绑定 File *multipart.FileHeader 类型,而无法绑定 File []*multipart.FileHeader 类型。

To Reproduce

type UserForm struct {
	Name string                  `validate:"required"`
	File []*multipart.FileHeader `validate:"required"`
}

data, err := validate.FromRequest(ctx.Request().Origin())
if err != nil {
	panic(err)
}

v := data.Create()
v.AddRule("name", "required")
v.AddRule("file", "required")

if v.Validate() { // validate ok
	// safeData := v.SafeData()
	userForm := &UserForm{}
	if err := v.BindSafeData(userForm); err != nil {
		panic(err)
	}

	// do something ...
	fmt.Println(userForm.File)
} else {
	fmt.Println(v.Errors)       // all error messages
	fmt.Println(v.Errors.One()) // returns a random error message text
}
curl --location '127.0.0.1:3000' \
--form 'name="test"' \
--form 'file=@"1.pdf"' \
--form 'file=@"2.pdf"'

Expected behavior

BindSafeData 支持绑定 File []*multipart.FileHeader 类型。

Screenshots

image image

Additional context

No