/
required.go
29 lines (25 loc) · 945 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
package validation
// Required is a validation rule that checks if a value is not empty.
// A value is considered not empty if
// - integer, float: not zero
// - bool: true
// - string, array, slice, map: len() > 0
// - interface, pointer: not nil and the referenced value is not empty
// - any other types
var Required = &requiredRule{skipNil: false, code: 1202}
// NilOrNotEmpty checks if a value is a nil pointer or a value that is not empty.
// NilOrNotEmpty differs from Required in that it treats a nil pointer as valid.
var NilOrNotEmpty = &requiredRule{skipNil: true, code: 1202}
type requiredRule struct {
skipNil bool
code int
}
// Validate checks if the given value is valid or not.
func (v *requiredRule) Validate(value interface{}) (code int, args []interface{}) {
value, isNil := Indirect(value)
if v.skipNil && !isNil && IsEmpty(value) || !v.skipNil && (isNil || IsEmpty(value)) {
code = v.code
return
}
return
}