github.com/gogf/gf@v1.16.9/util/gvalid/gvalid_validator_rule_required.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gvalid
     8  
     9  import (
    10  	"github.com/gogf/gf/internal/empty"
    11  	"github.com/gogf/gf/util/gconv"
    12  	"github.com/gogf/gf/util/gutil"
    13  	"reflect"
    14  	"strings"
    15  )
    16  
    17  // checkRequired checks `value` using required rules.
    18  // It also supports require checks for `value` of type: slice, map.
    19  func (v *Validator) checkRequired(value interface{}, ruleKey, rulePattern string, dataMap map[string]interface{}) bool {
    20  	required := false
    21  	switch ruleKey {
    22  	// Required.
    23  	case "required":
    24  		required = true
    25  
    26  	// Required unless all given field and its value are equal.
    27  	// Example: required-if: id,1,age,18
    28  	case "required-if":
    29  		required = false
    30  		var (
    31  			array      = strings.Split(rulePattern, ",")
    32  			foundValue interface{}
    33  		)
    34  		// It supports multiple field and value pairs.
    35  		if len(array)%2 == 0 {
    36  			for i := 0; i < len(array); {
    37  				tk := array[i]
    38  				tv := array[i+1]
    39  				_, foundValue = gutil.MapPossibleItemByKey(dataMap, tk)
    40  				if strings.Compare(tv, gconv.String(foundValue)) == 0 {
    41  					required = true
    42  					break
    43  				}
    44  				i += 2
    45  			}
    46  		}
    47  
    48  	// Required unless all given field and its value are not equal.
    49  	// Example: required-unless: id,1,age,18
    50  	case "required-unless":
    51  		required = true
    52  		var (
    53  			array      = strings.Split(rulePattern, ",")
    54  			foundValue interface{}
    55  		)
    56  		// It supports multiple field and value pairs.
    57  		if len(array)%2 == 0 {
    58  			for i := 0; i < len(array); {
    59  				tk := array[i]
    60  				tv := array[i+1]
    61  				_, foundValue = gutil.MapPossibleItemByKey(dataMap, tk)
    62  				if strings.Compare(tv, gconv.String(foundValue)) == 0 {
    63  					required = false
    64  					break
    65  				}
    66  
    67  				i += 2
    68  			}
    69  		}
    70  
    71  	// Required if any of given fields are not empty.
    72  	// Example: required-with:id,name
    73  	case "required-with":
    74  		required = false
    75  		var (
    76  			array      = strings.Split(rulePattern, ",")
    77  			foundValue interface{}
    78  		)
    79  		for i := 0; i < len(array); i++ {
    80  			_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
    81  			if !empty.IsEmpty(foundValue) {
    82  				required = true
    83  				break
    84  			}
    85  		}
    86  
    87  	// Required if all of given fields are not empty.
    88  	// Example: required-with:id,name
    89  	case "required-with-all":
    90  		required = true
    91  		var (
    92  			array      = strings.Split(rulePattern, ",")
    93  			foundValue interface{}
    94  		)
    95  		for i := 0; i < len(array); i++ {
    96  			_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
    97  			if empty.IsEmpty(foundValue) {
    98  				required = false
    99  				break
   100  			}
   101  		}
   102  
   103  	// Required if any of given fields are empty.
   104  	// Example: required-with:id,name
   105  	case "required-without":
   106  		required = false
   107  		var (
   108  			array      = strings.Split(rulePattern, ",")
   109  			foundValue interface{}
   110  		)
   111  		for i := 0; i < len(array); i++ {
   112  			_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
   113  			if empty.IsEmpty(foundValue) {
   114  				required = true
   115  				break
   116  			}
   117  		}
   118  
   119  	// Required if all of given fields are empty.
   120  	// Example: required-with:id,name
   121  	case "required-without-all":
   122  		required = true
   123  		var (
   124  			array      = strings.Split(rulePattern, ",")
   125  			foundValue interface{}
   126  		)
   127  		for i := 0; i < len(array); i++ {
   128  			_, foundValue = gutil.MapPossibleItemByKey(dataMap, array[i])
   129  			if !empty.IsEmpty(foundValue) {
   130  				required = false
   131  				break
   132  			}
   133  		}
   134  	}
   135  	if required {
   136  		reflectValue := reflect.ValueOf(value)
   137  		for reflectValue.Kind() == reflect.Ptr {
   138  			reflectValue = reflectValue.Elem()
   139  		}
   140  		switch reflectValue.Kind() {
   141  		case reflect.String, reflect.Map, reflect.Array, reflect.Slice:
   142  			return reflectValue.Len() != 0
   143  		}
   144  		return gconv.String(value) != ""
   145  	} else {
   146  		return true
   147  	}
   148  }