From 9e182173624d3a712918fc7d3dd5582921392f1f Mon Sep 17 00:00:00 2001 From: Wenbo Han Date: Wed, 3 Jul 2024 17:49:23 +0800 Subject: [PATCH] fix:[#450] The image rule of validation is wrong (#532) --- support/constant.go | 2 +- validation/validator.go | 25 ++++++++++- validation/validator_test.go | 82 ++++++++++++++++++------------------ 3 files changed, 65 insertions(+), 44 deletions(-) diff --git a/support/constant.go b/support/constant.go index 94a705592..3431bab7e 100644 --- a/support/constant.go +++ b/support/constant.go @@ -1,6 +1,6 @@ package support -const Version string = "v1.14.1" +const Version string = "v1.14.2" const ( EnvRuntime = "runtime" diff --git a/validation/validator.go b/validation/validator.go index 0149cb843..cd04319c9 100644 --- a/validation/validator.go +++ b/validation/validator.go @@ -1,6 +1,7 @@ package validation import ( + "mime/multipart" "reflect" "github.com/gookit/validate" @@ -39,7 +40,29 @@ func (v *Validator) Bind(ptr any) error { return err } - return decoder.Decode(data) + if err := decoder.Decode(data); err != nil { + return err + } + + ptrValue := reflect.Indirect(reflect.ValueOf(ptr)) + if ptrValue.Type().Kind() == reflect.Struct { + for i := 0; i < ptrValue.Type().NumField(); i++ { + if !ptrValue.Type().Field(i).IsExported() { + continue + } + formTag := ptrValue.Type().Field(i).Tag.Get("form") + if formTag != "" && ptrValue.Type().Field(i).Type == reflect.TypeOf(new(multipart.FileHeader)) { + if raw, exist := v.instance.Raw(formTag); exist { + rawType := reflect.TypeOf(raw) + if rawType == reflect.TypeOf(new(multipart.FileHeader)) { + ptrValue.Field(i).Set(reflect.ValueOf(raw)) + } + } + } + } + } + + return nil } func (v *Validator) Errors() httpvalidate.Errors { diff --git a/validation/validator_test.go b/validation/validator_test.go index 229dda20b..1a9bde90b 100644 --- a/validation/validator_test.go +++ b/validation/validator_test.go @@ -25,51 +25,50 @@ func TestBind(t *testing.T) { } tests := []struct { - name string - data validate.DataFace - rules map[string]string - expectData Data - expectErr error + name string + data validate.DataFace + rules map[string]string + assert func(data Data) }{ { name: "success when data is map and key is lowercase", data: validate.FromMap(map[string]any{"a": "aa"}), rules: map[string]string{"a": "required"}, - expectData: Data{ - A: "aa", + assert: func(data Data) { + assert.Equal(t, "aa", data.A) }, }, { name: "success when data is map and key is int", data: validate.FromMap(map[string]any{"b": 1}), rules: map[string]string{"b": "required"}, - expectData: Data{ - B: 1, + assert: func(data Data) { + assert.Equal(t, 1, data.B) }, }, { name: "success when data is map and cast key", data: validate.FromMap(map[string]any{"b": "1"}), rules: map[string]string{"b": "required"}, - expectData: Data{ - B: 1, + assert: func(data Data) { + assert.Equal(t, 1, data.B) }, }, { name: "success when data is map, key is lowercase and has errors", data: validate.FromMap(map[string]any{"a": "aa", "c": "cc"}), rules: map[string]string{"a": "required", "b": "required"}, - expectData: Data{ - A: "", - C: "", + assert: func(data Data) { + assert.Equal(t, "", data.A) + assert.Equal(t, "", data.C) }, }, { name: "success when data is map and key is uppercase", data: validate.FromMap(map[string]any{"A": "aa"}), rules: map[string]string{"A": "required"}, - expectData: Data{ - A: "aa", + assert: func(data Data) { + assert.Equal(t, "aa", data.A) }, }, { @@ -85,8 +84,8 @@ func TestBind(t *testing.T) { return data }(), rules: map[string]string{"A": "required"}, - expectData: Data{ - A: "aa", + assert: func(data Data) { + assert.Equal(t, "aa", data.A) }, }, { @@ -101,8 +100,10 @@ func TestBind(t *testing.T) { return data }(), - rules: map[string]string{"a": "required"}, - expectData: Data{}, + rules: map[string]string{"a": "required"}, + assert: func(data Data) { + assert.Equal(t, "", data.A) + }, }, { name: "empty when data is struct and key is struct", @@ -118,8 +119,10 @@ func TestBind(t *testing.T) { return data }(), - rules: map[string]string{"d.a": "required"}, - expectData: Data{}, + rules: map[string]string{"d.a": "required"}, + assert: func(data Data) { + assert.Equal(t, "", data.A) + }, }, { name: "success when data is get request", @@ -132,8 +135,8 @@ func TestBind(t *testing.T) { return data }(), rules: map[string]string{"a": "required"}, - expectData: Data{ - A: "aa", + assert: func(data Data) { + assert.Equal(t, "aa", data.A) }, }, { @@ -147,8 +150,8 @@ func TestBind(t *testing.T) { return data }(), rules: map[string]string{"b": "required"}, - expectData: Data{ - B: 1, + assert: func(data Data) { + assert.Equal(t, 1, data.B) }, }, { @@ -163,8 +166,8 @@ func TestBind(t *testing.T) { return data }(), rules: map[string]string{"a": "required"}, - expectData: Data{ - A: "aa", + assert: func(data Data) { + assert.Equal(t, "aa", data.A) }, }, { @@ -177,16 +180,14 @@ func TestBind(t *testing.T) { return data }(), rules: map[string]string{"a": "required", "file": "required"}, - expectData: func() Data { + assert: func(data Data) { request := buildRequest(t) - _, fileHeader, _ := request.FormFile("file") - data := Data{ - A: "aa", - File: fileHeader, - } + _, file, _ := request.FormFile("file") - return data - }(), + assert.Equal(t, "aa", data.A) + assert.NotNil(t, data.File) + assert.Equal(t, file.Filename, data.File.Filename) + }, }, } @@ -198,12 +199,9 @@ func TestBind(t *testing.T) { var data Data err = validator.Bind(&data) - assert.Nil(t, test.expectErr, err) - assert.Equal(t, test.expectData.A, data.A) - assert.Equal(t, test.expectData.B, data.B) - assert.Equal(t, test.expectData.C, data.C) - assert.Equal(t, test.expectData.D, data.D) - assert.Equal(t, test.expectData.File == nil, data.File == nil) + assert.Nil(t, err) + + test.assert(data) }) } }