Skip to content

Commit

Permalink
fix issue #2503
Browse files Browse the repository at this point in the history
  • Loading branch information
gqcn committed Feb 4, 2024
1 parent cc79d23 commit 5929698
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
3 changes: 2 additions & 1 deletion util/gvalid/gvalid_validator_check_struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
continue
}
if field.IsEmbedded() {
// The attributes of embedded struct are considered as direct attributes of its parent struct.
if err = v.doCheckStruct(ctx, field.Value); err != nil {
// It merges the errors into single error map.
for k, m := range err.(*validationError).errors {
Expand All @@ -257,7 +258,7 @@ func (v *Validator) doCheckStruct(ctx context.Context, object interface{}) Error
value = getPossibleValueFromMap(
inputParamMap, field.Name(), fieldToAliasNameMap[field.Name()],
)
if value == nil {
if empty.IsNil(value) {
switch field.Kind() {
case reflect.Map, reflect.Ptr, reflect.Slice, reflect.Array:
// Nothing to do.
Expand Down
2 changes: 1 addition & 1 deletion util/gvalid/gvalid_validator_check_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

type doCheckValueInput struct {
Name string // Name specifies the name of parameter `value`.
Name string // Name specifies the name of parameter `value`, which might be the custom tag name of the parameter.
Value interface{} // Value specifies the value for the rules to be validated.
ValueType reflect.Type // ValueType specifies the type of the value, mainly used for value type id retrieving.
Rule string // Rule specifies the validation rules string, like "required", "required|between:1,100", etc.
Expand Down
36 changes: 36 additions & 0 deletions util/gvalid/gvalid_z_unit_issue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gvalid_test

import (
"context"
"testing"

"github.com/gogf/gf/v2/util/gvalid"
)

type Foo struct {
Bar *Bar `p:"bar" v:"required-without:Baz"`
Baz *Baz `p:"baz" v:"required-without:Bar"`
}
type Bar struct {
BarKey string `p:"bar_key" v:"required"`
}
type Baz struct {
BazKey string `p:"baz_key" v:"required"`
}

// https://github.com/gogf/gf/issues/2503
func Test_Issue2503(t *testing.T) {
foo := &Foo{
Bar: &Bar{BarKey: "value"},
}
err := gvalid.New().Data(foo).Run(context.Background())
if err != nil {
t.Fatal(err)
}
}
2 changes: 2 additions & 0 deletions util/gvalid/internal/builtin/builtin_required.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ func (r RuleRequired) Run(in RunInput) error {
return nil
}

// isRequiredEmpty checks and returns whether given value is empty string.
// Note that if given value is a zero integer, it will be considered as not empty.
func isRequiredEmpty(value interface{}) bool {
reflectValue := reflect.ValueOf(value)
for reflectValue.Kind() == reflect.Ptr {
Expand Down

0 comments on commit 5929698

Please # to comment.