-
Notifications
You must be signed in to change notification settings - Fork 4
/
requires_rules.go
71 lines (67 loc) · 2.21 KB
/
requires_rules.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
package galidator
// Returns false if one of the fields values is not empty, nil or zero and input is empty, nil or zero
//
// False means it is required and all validators have to check
func whenExistOneRequireRule(fields ...string) func(interface{}) func(interface{}) bool {
return func(all interface{}) func(interface{}) bool {
fieldsValues := getValues(all, fields...)
return func(input interface{}) bool {
inputIsNil := isEmptyNilZero(input)
for _, element := range fieldsValues {
if !isEmptyNilZero(element) && inputIsNil {
return false
}
}
return true
}
}
}
// Returns false if all of the fields values are not empty, nil or zero and input is empty, nil or zero
//
// False means it is required and all validators have to check
func whenExistAllRequireRule(fields ...string) func(interface{}) func(interface{}) bool {
return func(all interface{}) func(interface{}) bool {
fieldsValues := getValues(all, fields...)
return func(input interface{}) bool {
for _, element := range fieldsValues {
if isEmptyNilZero(element) {
return true
}
}
return !isEmptyNilZero(input)
}
}
}
// Returns false if one of the fields values is empty, nil or zero and input is empty, nil or zero
//
// False means it is required and all validators have to check
func whenNotExistOneRequireRule(fields ...string) func(interface{}) func(interface{}) bool {
return func(all interface{}) func(interface{}) bool {
fieldsValues := getValues(all, fields...)
return func(input interface{}) bool {
inputIsNil := isEmptyNilZero(input)
for _, element := range fieldsValues {
if isEmptyNilZero(element) && inputIsNil {
return false
}
}
return true
}
}
}
// Returns false if all of the fields values are empty, nil or zero and input is empty, nil or zero
//
// False means it is required and all validators have to check
func whenNotExistAllRequireRule(fields ...string) func(interface{}) func(interface{}) bool {
return func(all interface{}) func(interface{}) bool {
fieldsValues := getValues(all, fields...)
return func(input interface{}) bool {
for _, element := range fieldsValues {
if !isEmptyNilZero(element) {
return true
}
}
return !isEmptyNilZero(input)
}
}
}