-
Notifications
You must be signed in to change notification settings - Fork 0
/
required.go
34 lines (25 loc) · 830 Bytes
/
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
package checker
import "reflect"
// CheckerRequired is the name of the checker.
const CheckerRequired = "required"
// ResultRequired indicates that the required value is missing.
const ResultRequired Result = "REQUIRED"
// IsRequired checks if the given required value is present.
func IsRequired(v interface{}) Result {
return checkRequired(reflect.ValueOf(v), reflect.ValueOf(nil))
}
// makeRequired makes a checker function for required.
func makeRequired(_ string) CheckFunc {
return checkRequired
}
// checkRequired checks if the required value is present.
func checkRequired(value, _ reflect.Value) Result {
if value.IsZero() {
return ResultRequired
}
k := value.Kind()
if (k == reflect.Array || k == reflect.Map || k == reflect.Slice) && value.Len() == 0 {
return ResultRequired
}
return ResultValid
}