Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #2503 #3299

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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