Skip to content

Commit 6abbc57

Browse files
committedMay 26, 2022
fix issue gogf#1864
1 parent 39af6e5 commit 6abbc57

4 files changed

+40
-11
lines changed
 

‎util/gvalid/gvalid_validator_check_map.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ func (v *Validator) doCheckMap(ctx context.Context, params interface{}) Error {
101101
switch originTypeAndKind.OriginKind {
102102
case reflect.Map, reflect.Struct, reflect.Slice, reflect.Array:
103103
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
104-
Value: item,
105-
Type: originTypeAndKind.InputType,
106-
OriginKind: originTypeAndKind.OriginKind,
107-
ErrorMaps: errorMaps,
104+
Value: item,
105+
Type: originTypeAndKind.InputType,
106+
Kind: originTypeAndKind.OriginKind,
107+
ErrorMaps: errorMaps,
108108
})
109109
}
110110
// Bail feature.

‎util/gvalid/gvalid_validator_check_struct.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,11 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
250250
}
251251
switch field.OriginalKind() {
252252
case reflect.Map, reflect.Struct, reflect.Slice, reflect.Array:
253-
// Recursively check attribute struct/[]string/map/[]map.
253+
// Recursively check attribute slice/map.
254254
_, value = gutil.MapPossibleItemByKey(inputParamMap, field.Name())
255255
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
256256
Value: value,
257-
OriginKind: field.OriginalKind(),
257+
Kind: field.OriginalKind(),
258258
Type: field.Type().Type,
259259
ErrorMaps: errorMaps,
260260
ResultSequenceRules: &resultSequenceRules,

‎util/gvalid/gvalid_validator_check_value.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -540,13 +540,22 @@ func (v *Validator) doCheckSingleBuildInRules(ctx context.Context, in doCheckBui
540540
type doCheckValueRecursivelyInput struct {
541541
Value interface{} // Value to be validated.
542542
Type reflect.Type // Struct/map/slice type which to be recursively validated.
543-
OriginKind reflect.Kind // Struct/map/slice kind to be asserted in following switch case.
543+
Kind reflect.Kind // Struct/map/slice kind to be asserted in following switch case.
544544
ErrorMaps map[string]map[string]error // The validated failed error map.
545545
ResultSequenceRules *[]fieldRule // The validated failed rule in sequence.
546546
}
547547

548548
func (v *Validator) doCheckValueRecursively(ctx context.Context, in doCheckValueRecursivelyInput) {
549-
switch in.OriginKind {
549+
switch in.Kind {
550+
case reflect.Ptr:
551+
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
552+
Value: in.Value,
553+
Type: in.Type.Elem(),
554+
Kind: in.Type.Elem().Kind(),
555+
ErrorMaps: in.ErrorMaps,
556+
ResultSequenceRules: in.ResultSequenceRules,
557+
})
558+
550559
case reflect.Struct:
551560
// Ignore data, rules and messages from parent.
552561
validator := v.Clone()
@@ -572,7 +581,7 @@ func (v *Validator) doCheckValueRecursively(ctx context.Context, in doCheckValue
572581
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
573582
Value: item,
574583
Type: mapTypeElem,
575-
OriginKind: mapTypeKind,
584+
Kind: mapTypeKind,
576585
ErrorMaps: in.ErrorMaps,
577586
ResultSequenceRules: in.ResultSequenceRules,
578587
})
@@ -596,7 +605,7 @@ func (v *Validator) doCheckValueRecursively(ctx context.Context, in doCheckValue
596605
v.doCheckValueRecursively(ctx, doCheckValueRecursivelyInput{
597606
Value: item,
598607
Type: in.Type.Elem(),
599-
OriginKind: in.Type.Elem().Kind(),
608+
Kind: in.Type.Elem().Kind(),
600609
ErrorMaps: in.ErrorMaps,
601610
ResultSequenceRules: in.ResultSequenceRules,
602611
})

‎util/gvalid/gvalid_z_unit_feature_recursive_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"testing"
1111

1212
"github.com/gogf/gf/v2/frame/g"
13-
1413
"github.com/gogf/gf/v2/test/gtest"
1514
)
1615

@@ -274,6 +273,27 @@ func Test_CheckStruct_Recursively_SliceAttribute(t *testing.T) {
274273
err := g.Validator().Assoc(data).Data(teacher).Run(ctx)
275274
t.Assert(err, `Student Name is required`)
276275
})
276+
277+
// https://github.com/gogf/gf/issues/1864
278+
gtest.C(t, func(t *gtest.T) {
279+
type Student struct {
280+
Name string `v:"required"`
281+
Age int
282+
}
283+
type Teacher struct {
284+
Name string
285+
Students []*Student
286+
}
287+
var (
288+
teacher = Teacher{}
289+
data = g.Map{
290+
"name": "john",
291+
"students": `[{"age":2},{"name":"jack", "age":4}]`,
292+
}
293+
)
294+
err := g.Validator().Assoc(data).Data(teacher).Run(ctx)
295+
t.Assert(err, `The Name field is required`)
296+
})
277297
}
278298

279299
func Test_CheckStruct_Recursively_SliceAttribute_WithTypeAlias(t *testing.T) {

0 commit comments

Comments
 (0)