diff --git a/util/gvalid/internal/builtin/builtin_required_if.go b/util/gvalid/internal/builtin/builtin_required_if.go index 45123c06688..71299eff31c 100644 --- a/util/gvalid/internal/builtin/builtin_required_if.go +++ b/util/gvalid/internal/builtin/builtin_required_if.go @@ -10,15 +10,17 @@ import ( "errors" "strings" + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/util/gutil" ) // RuleRequiredIf implements `required-if` rule: -// Required unless all given field and its value are equal. +// Required if any of given field and its value are equal. // // Format: required-if:field,value,... -// Example: required-if: id,1,age,18 +// Example: required-if:id,1,age,18 type RuleRequiredIf struct{} func init() { @@ -40,24 +42,31 @@ func (r RuleRequiredIf) Run(in RunInput) error { foundValue interface{} dataMap = in.Data.Map() ) + if len(array)%2 != 0 { + return gerror.NewCodef( + gcode.CodeInvalidParameter, + `invalid "%s" rule pattern: %s`, + r.Name(), + in.RulePattern, + ) + } // It supports multiple field and value pairs. - if len(array)%2 == 0 { - for i := 0; i < len(array); { - tk := array[i] - tv := array[i+1] - _, foundValue = gutil.MapPossibleItemByKey(dataMap, tk) - if in.Option.CaseInsensitive { - required = strings.EqualFold(tv, gconv.String(foundValue)) - } else { - required = strings.Compare(tv, gconv.String(foundValue)) == 0 - } - if required { - break - } - i += 2 + for i := 0; i < len(array); { + var ( + tk = array[i] + tv = array[i+1] + ) + _, foundValue = gutil.MapPossibleItemByKey(dataMap, tk) + if in.Option.CaseInsensitive { + required = strings.EqualFold(tv, gconv.String(foundValue)) + } else { + required = strings.Compare(tv, gconv.String(foundValue)) == 0 + } + if required { + break } + i += 2 } - if required && isRequiredEmpty(in.Value.Val()) { return errors.New(in.Message) } diff --git a/util/gvalid/internal/builtin/builtin_required_if_all.go b/util/gvalid/internal/builtin/builtin_required_if_all.go index ad015adcc91..4585a6e16e0 100644 --- a/util/gvalid/internal/builtin/builtin_required_if_all.go +++ b/util/gvalid/internal/builtin/builtin_required_if_all.go @@ -10,6 +10,8 @@ import ( "errors" "strings" + "github.com/gogf/gf/v2/errors/gcode" + "github.com/gogf/gf/v2/errors/gerror" "github.com/gogf/gf/v2/util/gconv" "github.com/gogf/gf/v2/util/gutil" ) @@ -18,7 +20,7 @@ import ( // Required if all given field and its value are equal. // // Format: required-if-all:field,value,... -// Example: required-if-all: id,1,age,18 +// Example: required-if-all:id,1,age,18 type RuleRequiredIfAllEq struct{} func init() { @@ -40,33 +42,32 @@ func (r RuleRequiredIfAllEq) Run(in RunInput) error { foundValue interface{} dataMap = in.Data.Map() ) - // It supports multiple field and value pairs. - if len(array)%2 == 0 { - eqMap := make(map[string]bool) - for i := 0; i < len(array); { - tk := array[i] - tv := array[i+1] - var eq bool - _, foundValue = gutil.MapPossibleItemByKey(dataMap, tk) - if in.Option.CaseInsensitive { - eq = strings.EqualFold(tv, gconv.String(foundValue)) - } else { - eq = strings.Compare(tv, gconv.String(foundValue)) == 0 - } - eqMap[tk] = eq - if !eq { - break - } - i += 2 + if len(array)%2 != 0 { + return gerror.NewCodef( + gcode.CodeInvalidParameter, + `invalid "%s" rule pattern: %s`, + r.Name(), + in.RulePattern, + ) + } + for i := 0; i < len(array); { + var ( + tk = array[i] + tv = array[i+1] + eq bool + ) + _, foundValue = gutil.MapPossibleItemByKey(dataMap, tk) + if in.Option.CaseInsensitive { + eq = strings.EqualFold(tv, gconv.String(foundValue)) + } else { + eq = strings.Compare(tv, gconv.String(foundValue)) == 0 } - for _, eq := range eqMap { - if !eq { - required = false - break - } + if !eq { + required = false + break } + i += 2 } - if required && isRequiredEmpty(in.Value.Val()) { return errors.New(in.Message) }