mirror of
https://gitee.com/johng/gf
synced 2026-06-30 11:05:11 +08:00
add required* rules checks for map/slice
This commit is contained in:
@ -158,7 +158,7 @@ func (v *Validator) doCheckBuildInRules(
|
||||
"required-with-all",
|
||||
"required-without",
|
||||
"required-without-all":
|
||||
match = v.checkRequired(valueStr, ruleKey, rulePattern, dataMap)
|
||||
match = v.checkRequired(value, ruleKey, rulePattern, dataMap)
|
||||
|
||||
// Length rules.
|
||||
// It also supports length of unicode string.
|
||||
|
||||
@ -7,11 +7,14 @@
|
||||
package gvalid
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// checkRequired checks <value> using required rules.
|
||||
func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[string]string) bool {
|
||||
// It also supports require checks for `value` of type: slice, map.
|
||||
func (v *Validator) checkRequired(value interface{}, ruleKey, rulePattern string, params map[string]string) bool {
|
||||
required := false
|
||||
switch ruleKey {
|
||||
// Required.
|
||||
@ -22,7 +25,7 @@ func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[str
|
||||
// Example: required-if: id,1,age,18
|
||||
case "required-if":
|
||||
required = false
|
||||
array := strings.Split(ruleVal, ",")
|
||||
array := strings.Split(rulePattern, ",")
|
||||
// It supports multiple field and value pairs.
|
||||
if len(array)%2 == 0 {
|
||||
for i := 0; i < len(array); {
|
||||
@ -42,7 +45,7 @@ func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[str
|
||||
// Example: required-unless: id,1,age,18
|
||||
case "required-unless":
|
||||
required = true
|
||||
array := strings.Split(ruleVal, ",")
|
||||
array := strings.Split(rulePattern, ",")
|
||||
// It supports multiple field and value pairs.
|
||||
if len(array)%2 == 0 {
|
||||
for i := 0; i < len(array); {
|
||||
@ -62,7 +65,7 @@ func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[str
|
||||
// Example: required-with:id,name
|
||||
case "required-with":
|
||||
required = false
|
||||
array := strings.Split(ruleVal, ",")
|
||||
array := strings.Split(rulePattern, ",")
|
||||
for i := 0; i < len(array); i++ {
|
||||
if params[array[i]] != "" {
|
||||
required = true
|
||||
@ -74,7 +77,7 @@ func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[str
|
||||
// Example: required-with:id,name
|
||||
case "required-with-all":
|
||||
required = true
|
||||
array := strings.Split(ruleVal, ",")
|
||||
array := strings.Split(rulePattern, ",")
|
||||
for i := 0; i < len(array); i++ {
|
||||
if params[array[i]] == "" {
|
||||
required = false
|
||||
@ -86,7 +89,7 @@ func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[str
|
||||
// Example: required-with:id,name
|
||||
case "required-without":
|
||||
required = false
|
||||
array := strings.Split(ruleVal, ",")
|
||||
array := strings.Split(rulePattern, ",")
|
||||
for i := 0; i < len(array); i++ {
|
||||
if params[array[i]] == "" {
|
||||
required = true
|
||||
@ -98,7 +101,7 @@ func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[str
|
||||
// Example: required-with:id,name
|
||||
case "required-without-all":
|
||||
required = true
|
||||
array := strings.Split(ruleVal, ",")
|
||||
array := strings.Split(rulePattern, ",")
|
||||
for i := 0; i < len(array); i++ {
|
||||
if params[array[i]] != "" {
|
||||
required = false
|
||||
@ -107,7 +110,15 @@ func (v *Validator) checkRequired(value, ruleKey, ruleVal string, params map[str
|
||||
}
|
||||
}
|
||||
if required {
|
||||
return !(value == "")
|
||||
reflectValue := reflect.ValueOf(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
|
||||
}
|
||||
return gconv.String(value) != ""
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
|
||||
@ -173,13 +173,13 @@ func ExampleRegisterRule_OverwriteRequired() {
|
||||
return nil
|
||||
})
|
||||
fmt.Println(gvalid.Check("", "required", "It's required"))
|
||||
fmt.Println(gvalid.Check([]string{}, "required", "It's required"))
|
||||
fmt.Println(gvalid.Check(map[string]int{}, "required", "It's required"))
|
||||
fmt.Println(gvalid.Check(0, "required", "It's required"))
|
||||
fmt.Println(gvalid.Check(false, "required", "It's required"))
|
||||
gvalid.DeleteRule(rule)
|
||||
fmt.Println("rule deleted")
|
||||
fmt.Println(gvalid.Check("", "required", "It's required"))
|
||||
fmt.Println(gvalid.Check([]string{}, "required", "It's required"))
|
||||
fmt.Println(gvalid.Check(map[string]int{}, "required", "It's required"))
|
||||
fmt.Println(gvalid.Check(0, "required", "It's required"))
|
||||
fmt.Println(gvalid.Check(false, "required", "It's required"))
|
||||
// Output:
|
||||
// It's required
|
||||
// It's required
|
||||
|
||||
Reference in New Issue
Block a user