-
Notifications
You must be signed in to change notification settings - Fork 55
/
valid.go
109 lines (96 loc) · 2.25 KB
/
valid.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Package zvalid data verification
package zvalid
import (
"container/list"
"errors"
"strconv"
)
type (
// Engine valid engine
Engine struct {
queue *list.List
setRawValue bool
err error
name string
value string
valueInt int
valueFloat float64
sep string
silent bool
defaultValue interface{}
result bool
}
queueT func(v *Engine) *Engine
)
var (
// ErrNoValidationValueSet no verification value set
ErrNoValidationValueSet = errors.New("未设置验证值")
)
// New valid
func New() Engine {
return Engine{
queue: list.New(),
}
}
// Int use int new valid
func Int(value int, name ...string) Engine {
return Text(strconv.FormatInt(int64(value), 10), name...)
}
// Text use int new valid
func Text(value string, name ...string) Engine {
var obj Engine
obj.value = value
obj.setRawValue = true
obj.queue = list.New()
if len(name) > 0 {
obj.name = name[0]
}
return obj
}
// Required Must have a value (zero values other than "" are allowed). If this rule is not used, when the parameter value is "", data validation does not take effect by default
func (v Engine) Required(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
if v.value == "" {
v.err = setError(v, "不能为空", customError...)
}
return v
})
}
// Customize customize valid
func (v Engine) Customize(fn func(rawValue string, err error) (newValue string, newErr error)) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
v.value, v.err = fn(v.value, v.err)
return v
}, true)
}
func pushQueue(v *Engine, fn queueT, DisableCheckErr ...bool) Engine {
pFn := fn
if !(len(DisableCheckErr) > 0 && DisableCheckErr[0]) {
pFn = func(v *Engine) *Engine {
if v.err != nil {
return v
}
return fn(v)
}
}
queue := list.New()
queue.PushBackList(v.queue)
queue.PushBack(pFn)
v.queue = queue
return *v
}
func ignore(v *Engine) bool {
return v.err != nil || v.value == ""
}
func notEmpty(v *Engine) bool {
return v.value != ""
}
func setError(v *Engine, msg string, customError ...string) error {
if len(customError) > 0 && customError[0] != "" {
return errors.New(customError[0])
}
if v.name != "" {
msg = v.name + msg
}
return errors.New(msg)
}