-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
190 lines (164 loc) · 4.22 KB
/
validate.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package u
import (
"fmt"
"reflect"
"regexp"
"strings"
)
type vMap struct{
Errors []string
MapRules map[string]interface{}
MapData map[string]interface{}
Val interface{}
}
type vStruct struct{
Errors []string
Val interface{}
}
func ValidateMap(val interface{}) *vMap {
return &vMap{
MapRules: make(map[string]interface{}),
MapData: val.(map[string]interface{}),
}
}
func ValidateStruct(val interface{}) *vStruct {
return &vStruct{Val:val}
}
// Validate validate struct
func (receiver *vStruct)Validate() bool {
var errors []string
vo := reflect.ValueOf(receiver.Val)
t := vo.Type()
for i:=0; i<t.NumField(); i++ {
tagRule := t.Field(i).Tag.Get("v")
rules := strings.Split(tagRule, "|")
field := t.Field(i).Name
val := vo.Field(i).String()
// ergodic
for _, ruleItem := range rules {
rule := strings.Split(ruleItem, "#")[0]
// required rule
if rule == "required" {
ok, errMsg := required(field, ruleItem, val)
if !ok { errors = append(errors, errMsg) }
}
// minLen:n
if matched, _ := regexp.MatchString(`minLen:(\d+)`, rule); matched == true{
ok, errMsg := minLen(field, ruleItem, val)
if !ok { errors = append(errors, errMsg) }
}
// maxLen:n
if matched, _ := regexp.MatchString(`maxLen:(\d+)`, rule); matched == true{
ok, errMsg := maxLen(field, ruleItem, val)
if !ok { errors = append(errors, errMsg) }
}
}
}
receiver.Errors = errors
if len(errors) == 0 {
return true
} else {
return false
}
}
// Validate validate map
func (receiver *vMap) Validate() bool {
var errors []string
for field, val := range receiver.MapData {
if receiver.MapRules[field] != nil {
tagRule := receiver.MapRules[field].(string)
rules := strings.Split(tagRule, "|")
for _, ruleItem := range rules {
rule := strings.Split(ruleItem, "#")[0]
// required rule
if rule == "required" {
ok, errMsg := required(field, ruleItem, val.(string))
if !ok { errors = append(errors, errMsg) }
}
// minLen:n
if matched, _ := regexp.MatchString(`minLen:(\d+)`, rule); matched == true{
ok, errMsg := minLen(field, ruleItem, val.(string))
if !ok { errors = append(errors, errMsg) }
}
// maxLen:n
if matched, _ := regexp.MatchString(`maxLen:(\d+)`, rule); matched == true{
ok, errMsg := maxLen(field, ruleItem, val.(string))
if !ok { errors = append(errors, errMsg) }
}
}
}
}
receiver.Errors = errors
if len(errors) == 0 {
return true
} else {
return false
}
}
// AddRule add map rule
func (receiver *vMap)AddRule(field string, rule string) {
receiver.MapRules[field] = rule
}
// FirstError return first err msg
func (receiver *vMap) FirstError() string {
if len(receiver.Errors) == 0 {
return ""
}
return receiver.Errors[0]
}
// FirstError return first err msg
func (receiver *vStruct) FirstError() string {
if len(receiver.Errors) == 0 {
return ""
}
return receiver.Errors[0]
}
// maxLen:n
func maxLen(fieldName ,rule string, val string) (bool, string) {
var customMsg string
if len(strings.Split(rule, "#")) > 1 {
customMsg = strings.Split(rule, "#")[1]
}
compile, _ := regexp.Compile(`maxLen:(\d+)`)
if compile.MatchString(rule) {
ruleNum := ToInt(compile.FindStringSubmatch(rule)[1])
if len(val) > ruleNum {
if len(customMsg) == 0 {
return false, fmt.Sprintf("%s %s", fieldName, "maxLen")
}else {
return false, fmt.Sprintf("%s %s", fieldName, customMsg)
}
}
}
return true, ""
}
// minLen:n
func minLen(fieldName , rule string, val string) (bool, string) {
var customMsg string
if len(strings.Split(rule, "#")) > 1 {
customMsg = strings.Split(rule, "#")[0]
}
compile, _ := regexp.Compile(`minLen:(\d+)`)
if compile.MatchString(rule) {
ruleNum := ToInt(compile.FindStringSubmatch(rule)[1])
if len(val) < ruleNum {
if len(customMsg) == 0 {
return false, fmt.Sprintf("%s %s", fieldName, "minLen")
}else {
return false, fmt.Sprintf("%s %s", fieldName, customMsg)
}
}
}
return true, ""
}
// required
func required(fieldName ,customMsg string, val string) (bool, string) {
if len(val) == 0 {
if len(customMsg) == 0 {
return false, fmt.Sprintf("%s %s", fieldName, "required")
}else {
return false, fmt.Sprintf("%s %s", fieldName, customMsg)
}
}
return true, ""
}