-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.go
70 lines (57 loc) · 1.7 KB
/
common.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
package livr
import (
"errors"
"reflect"
)
// anyObject - rule for checking that validated value is not empty object.
func anyObject(...interface{}) Validation {
return func(value interface{}, builders ...interface{}) (interface{}, interface{}) {
if value == nil || value == "" {
return value, nil
}
if reflect.ValueOf(value).Kind() != reflect.Map {
return nil, errors.New("FORMAT_ERROR")
}
if isZero(reflect.ValueOf(value)) {
return nil, errors.New("FORMAT_ERROR")
}
return value, nil
}
}
// notEmpty - check that validated value is not empty if exists.
func notEmpty(...interface{}) Validation {
return func(value interface{}, builders ...interface{}) (interface{}, interface{}) {
if value == nil {
// TODO: return error
return nil, nil
}
if isZero(reflect.ValueOf(value)) {
return nil, errors.New("CANNOT_BE_EMPTY")
}
return value, nil
}
}
// notEmptyList - check that validated value is not empty list.
func notEmptyList(...interface{}) Validation {
return func(value interface{}, builders ...interface{}) (interface{}, interface{}) {
if value == nil || value == "" {
return nil, errors.New("CANNOT_BE_EMPTY")
}
if reflect.TypeOf(value).Kind() != reflect.Array && reflect.TypeOf(value).Kind() != reflect.Slice {
return nil, errors.New("FORMAT_ERROR")
}
if reflect.ValueOf(value).Len() == 0 {
return nil, errors.New("CANNOT_BE_EMPTY")
}
return value, nil
}
}
// required - checks that validated value exists and not empty.
func required(...interface{}) Validation {
return func(value interface{}, builders ...interface{}) (interface{}, interface{}) {
if value == nil || value == "" {
return nil, errors.New("REQUIRED")
}
return value, nil
}
}