-
Notifications
You must be signed in to change notification settings - Fork 2
/
notempty.go
42 lines (40 loc) · 1021 Bytes
/
notempty.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
package validator
import (
"errors"
"reflect"
)
func notEmpty(v reflect.Value, name, param string) error {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
if v.Len() == 0 {
return errors.New(name + " must not be empty")
}
return nil
case reflect.Bool:
if !v.Bool() {
return errors.New(name + " must not be false")
}
return nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Int() == 0 {
return errors.New(name + " must not be zero")
}
return nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
if v.Uint() == 0 {
return errors.New(name + " must not be zero")
}
return nil
case reflect.Float32, reflect.Float64:
if v.Float() == 0 {
return errors.New(name + " must not be zero")
}
return nil
case reflect.Interface, reflect.Ptr:
if v.IsNil() {
return errors.New(name + " must not be nil")
}
return nil
}
return UnsupportedError(name)
}