mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
fix: map converting in loop logic of validation for package gvalid (#3423)
This commit is contained in:
@ -32,12 +32,16 @@ func (r RuleIn) Message() string {
|
||||
}
|
||||
|
||||
func (r RuleIn) Run(in RunInput) error {
|
||||
var ok bool
|
||||
var (
|
||||
ok bool
|
||||
inputValueString = in.Value.String()
|
||||
)
|
||||
|
||||
for _, rulePattern := range gstr.SplitAndTrim(in.RulePattern, ",") {
|
||||
if in.Option.CaseInsensitive {
|
||||
ok = strings.EqualFold(in.Value.String(), strings.TrimSpace(rulePattern))
|
||||
ok = strings.EqualFold(inputValueString, strings.TrimSpace(rulePattern))
|
||||
} else {
|
||||
ok = strings.Compare(in.Value.String(), strings.TrimSpace(rulePattern)) == 0
|
||||
ok = strings.Compare(inputValueString, strings.TrimSpace(rulePattern)) == 0
|
||||
}
|
||||
if ok {
|
||||
return nil
|
||||
|
||||
@ -38,13 +38,14 @@ func (r RuleRequiredIf) Run(in RunInput) error {
|
||||
required = false
|
||||
array = strings.Split(in.RulePattern, ",")
|
||||
foundValue interface{}
|
||||
dataMap = in.Data.Map()
|
||||
)
|
||||
// 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(in.Data.Map(), tk)
|
||||
_, foundValue = gutil.MapPossibleItemByKey(dataMap, tk)
|
||||
if in.Option.CaseInsensitive {
|
||||
required = strings.EqualFold(tv, gconv.String(foundValue))
|
||||
} else {
|
||||
|
||||
@ -38,13 +38,15 @@ func (r RuleRequiredUnless) Run(in RunInput) error {
|
||||
required = true
|
||||
array = strings.Split(in.RulePattern, ",")
|
||||
foundValue interface{}
|
||||
dataMap = in.Data.Map()
|
||||
)
|
||||
|
||||
// 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(in.Data.Map(), tk)
|
||||
_, foundValue = gutil.MapPossibleItemByKey(dataMap, tk)
|
||||
if in.Option.CaseInsensitive {
|
||||
required = !strings.EqualFold(tv, gconv.String(foundValue))
|
||||
} else {
|
||||
|
||||
@ -38,9 +38,11 @@ func (r RuleRequiredWith) Run(in RunInput) error {
|
||||
required = false
|
||||
array = strings.Split(in.RulePattern, ",")
|
||||
foundValue interface{}
|
||||
dataMap = in.Data.Map()
|
||||
)
|
||||
|
||||
for i := 0; i < len(array); i++ {
|
||||
_, foundValue = gutil.MapPossibleItemByKey(in.Data.Map(), array[i])
|
||||
_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
|
||||
if !empty.IsEmpty(foundValue) {
|
||||
required = true
|
||||
break
|
||||
|
||||
@ -38,9 +38,11 @@ func (r RuleRequiredWithAll) Run(in RunInput) error {
|
||||
required = true
|
||||
array = strings.Split(in.RulePattern, ",")
|
||||
foundValue interface{}
|
||||
dataMap = in.Data.Map()
|
||||
)
|
||||
|
||||
for i := 0; i < len(array); i++ {
|
||||
_, foundValue = gutil.MapPossibleItemByKey(in.Data.Map(), array[i])
|
||||
_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
|
||||
if empty.IsEmpty(foundValue) {
|
||||
required = false
|
||||
break
|
||||
|
||||
@ -38,9 +38,11 @@ func (r RuleRequiredWithout) Run(in RunInput) error {
|
||||
required = false
|
||||
array = strings.Split(in.RulePattern, ",")
|
||||
foundValue interface{}
|
||||
dataMap = in.Data.Map()
|
||||
)
|
||||
|
||||
for i := 0; i < len(array); i++ {
|
||||
_, foundValue = gutil.MapPossibleItemByKey(in.Data.Map(), array[i])
|
||||
_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
|
||||
if empty.IsEmpty(foundValue) {
|
||||
required = true
|
||||
break
|
||||
|
||||
@ -38,9 +38,11 @@ func (r RuleRequiredWithoutAll) Run(in RunInput) error {
|
||||
required = true
|
||||
array = strings.Split(in.RulePattern, ",")
|
||||
foundValue interface{}
|
||||
dataMap = in.Data.Map()
|
||||
)
|
||||
|
||||
for i := 0; i < len(array); i++ {
|
||||
_, foundValue = gutil.MapPossibleItemByKey(in.Data.Map(), array[i])
|
||||
_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
|
||||
if !empty.IsEmpty(foundValue) {
|
||||
required = false
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user