Files
gf/util/gvalid/gvalid_validator_rule_required.go

165 lines
4.3 KiB
Go
Raw Normal View History

2021-01-12 10:46:39 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2020-05-10 10:56:11 +08:00
//
// 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
import (
2021-11-13 23:30:31 +08:00
"reflect"
"strings"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/internal/empty"
"github.com/gogf/gf/v2/util/gconv"
"github.com/gogf/gf/v2/util/gutil"
2020-05-10 10:56:11 +08:00
)
2021-11-22 14:41:33 +08:00
type checkRequiredInput struct {
Value interface{} // Value to be validated.
RuleKey string // RuleKey is like the "max" in rule "max: 6"
RulePattern string // RulePattern is like "6" in rule:"max:6"
DataMap map[string]interface{} // Parameter map.
CaseInsensitive bool // Case-Insensitive comparison.
}
// checkRequired checks `value` using required rules.
// It also supports require checks for `value` of type: slice, map.
2021-11-22 14:41:33 +08:00
func (v *Validator) checkRequired(in checkRequiredInput) bool {
2020-05-10 10:56:11 +08:00
required := false
2021-11-22 14:41:33 +08:00
switch in.RuleKey {
2020-05-10 16:48:00 +08:00
// Required.
2020-05-10 10:56:11 +08:00
case "required":
required = true
2020-05-10 16:48:00 +08:00
// Required unless all given field and its value are equal.
// Example: required-if: id,1,age,18
2020-05-10 10:56:11 +08:00
case "required-if":
required = false
var (
2021-11-22 14:41:33 +08:00
array = strings.Split(in.RulePattern, ",")
foundValue interface{}
)
2020-05-10 16:48:00 +08:00
// It supports multiple field and value pairs.
2020-05-10 10:56:11 +08:00
if len(array)%2 == 0 {
for i := 0; i < len(array); {
tk := array[i]
tv := array[i+1]
2021-11-22 14:41:33 +08:00
_, foundValue = gutil.MapPossibleItemByKey(in.DataMap, tk)
if in.CaseInsensitive {
required = strings.EqualFold(tv, gconv.String(foundValue))
} else {
required = strings.Compare(tv, gconv.String(foundValue)) == 0
}
if required {
break
2020-05-10 10:56:11 +08:00
}
i += 2
}
}
2020-05-10 16:48:00 +08:00
// Required unless all given field and its value are not equal.
// Example: required-unless: id,1,age,18
2020-05-10 10:56:11 +08:00
case "required-unless":
required = true
var (
2021-11-22 14:41:33 +08:00
array = strings.Split(in.RulePattern, ",")
foundValue interface{}
)
2020-05-10 16:48:00 +08:00
// It supports multiple field and value pairs.
2020-05-10 10:56:11 +08:00
if len(array)%2 == 0 {
for i := 0; i < len(array); {
tk := array[i]
tv := array[i+1]
2021-11-22 14:41:33 +08:00
_, foundValue = gutil.MapPossibleItemByKey(in.DataMap, tk)
if in.CaseInsensitive {
required = !strings.EqualFold(tv, gconv.String(foundValue))
} else {
required = strings.Compare(tv, gconv.String(foundValue)) != 0
}
if !required {
break
2020-05-10 10:56:11 +08:00
}
i += 2
}
}
2020-05-10 16:48:00 +08:00
// Required if any of given fields are not empty.
// Example: required-with:id,name
2020-05-10 10:56:11 +08:00
case "required-with":
required = false
var (
2021-11-22 14:41:33 +08:00
array = strings.Split(in.RulePattern, ",")
foundValue interface{}
)
2020-05-10 10:56:11 +08:00
for i := 0; i < len(array); i++ {
2021-11-22 14:41:33 +08:00
_, foundValue = gutil.MapPossibleItemByKey(in.DataMap, array[i])
if !empty.IsEmpty(foundValue) {
2020-05-10 10:56:11 +08:00
required = true
break
}
}
2021-09-27 22:52:14 +08:00
// Required if all given fields are not empty.
2020-05-10 16:48:00 +08:00
// Example: required-with:id,name
2020-05-10 10:56:11 +08:00
case "required-with-all":
required = true
var (
2021-11-22 14:41:33 +08:00
array = strings.Split(in.RulePattern, ",")
foundValue interface{}
)
2020-05-10 10:56:11 +08:00
for i := 0; i < len(array); i++ {
2021-11-22 14:41:33 +08:00
_, foundValue = gutil.MapPossibleItemByKey(in.DataMap, array[i])
if empty.IsEmpty(foundValue) {
2020-05-10 10:56:11 +08:00
required = false
break
}
}
2020-05-10 16:48:00 +08:00
// Required if any of given fields are empty.
// Example: required-with:id,name
2020-05-10 10:56:11 +08:00
case "required-without":
required = false
var (
2021-11-22 14:41:33 +08:00
array = strings.Split(in.RulePattern, ",")
foundValue interface{}
)
2020-05-10 10:56:11 +08:00
for i := 0; i < len(array); i++ {
2021-11-22 14:41:33 +08:00
_, foundValue = gutil.MapPossibleItemByKey(in.DataMap, array[i])
if empty.IsEmpty(foundValue) {
2020-05-10 10:56:11 +08:00
required = true
break
}
}
// Required if all given fields are empty.
2020-05-10 16:48:00 +08:00
// Example: required-with:id,name
2020-05-10 10:56:11 +08:00
case "required-without-all":
required = true
var (
2021-11-22 14:41:33 +08:00
array = strings.Split(in.RulePattern, ",")
foundValue interface{}
)
2020-05-10 10:56:11 +08:00
for i := 0; i < len(array); i++ {
2021-11-22 14:41:33 +08:00
_, foundValue = gutil.MapPossibleItemByKey(in.DataMap, array[i])
if !empty.IsEmpty(foundValue) {
2020-05-10 10:56:11 +08:00
required = false
break
}
}
}
if required {
2021-11-22 14:41:33 +08:00
reflectValue := reflect.ValueOf(in.Value)
for reflectValue.Kind() == reflect.Ptr {
reflectValue = reflectValue.Elem()
}
switch reflectValue.Kind() {
case reflect.String, reflect.Map, reflect.Array, reflect.Slice:
return reflectValue.Len() != 0
}
2021-11-22 14:41:33 +08:00
return gconv.String(in.Value) != ""
2020-05-10 10:56:11 +08:00
} else {
return true
}
}