Skip to content

Commit

Permalink
fix:[#450] The image rule of validation is wrong (#532)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Jul 3, 2024
1 parent 1b41800 commit 9e18217
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 44 deletions.
2 changes: 1 addition & 1 deletion support/constant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package support

const Version string = "v1.14.1"
const Version string = "v1.14.2"

const (
EnvRuntime = "runtime"
Expand Down
25 changes: 24 additions & 1 deletion validation/validator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package validation

import (
"mime/multipart"
"reflect"

"github.com/gookit/validate"
Expand Down Expand Up @@ -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 {
Expand Down
82 changes: 40 additions & 42 deletions validation/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
{
Expand All @@ -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)
},
},
}

Expand All @@ -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)
})
}
}
Expand Down

0 comments on commit 9e18217

Please sign in to comment.