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