forked from YaoApp/gou
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
176 lines (150 loc) · 3.83 KB
/
validation.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
package gou
import (
"fmt"
"net/mail"
"regexp"
"time"
"github.com/yaoapp/kun/any"
"github.com/yaoapp/kun/maps"
"github.com/yaoapp/kun/str"
)
// Validations 数据校验函数
var Validations = map[string]func(value interface{}, row maps.MapStrAny, args ...interface{}) bool{
"typof": ValidationTypeof, // 校验数值类型 string, integer, float, number, datetime, timestamp,
"min": ValidationMin, // 最小值
"max": ValidationMax, // 最大值
"enum": ValidationEnum, // 枚举型
"pattern": ValidationPattern, // 正则匹配
"minLength": ValidationMinLength, // 最小长度
"maxLength": ValidationMaxLength, // 最大长度
"email": ValidationEmail, // 邮箱地址
"mobile": ValidationMobile, // 手机号
}
// ValidationTypeof 校验数值类型
func ValidationTypeof(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
if len(args) < 1 {
return false
}
typ := str.Of(args[0])
switch typ {
case "string":
_, ok := value.(string)
return ok
case "integer":
return any.Of(value).IsInt()
case "float":
return any.Of(value).IsFloat()
case "number":
return any.Of(value).IsNumber()
case "datetime":
isDatetime := false
formats := []string{
"2006-01-02 15:04:05",
"2006-01-02T15:04:05",
}
valueStr := fmt.Sprintf("%v", value)
for _, format := range formats {
_, err := time.Parse(format, valueStr)
if err == nil {
isDatetime = true
}
}
return isDatetime
case "bool":
v := any.Of(value)
if v.IsInt() {
return v.Int() == 1 || v.Int() == 0
}
return v.IsBool()
}
return false
}
// ValidationMin 最小值
func ValidationMin(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
if len(args) < 1 {
return true
}
v := any.Of(value)
if v.IsInt() {
return v.Int() >= any.Of(args[0]).CInt()
} else if v.IsFloat() {
return v.Float() >= any.Of(args[0]).CFloat()
}
return false
}
// ValidationMax 最大值
func ValidationMax(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
if len(args) < 1 {
return true
}
v := any.Of(value)
if v.IsInt() {
return v.Int() <= any.Of(args[0]).CInt()
} else if v.IsFloat() {
return v.Float() <= any.Of(args[0]).CFloat()
}
return false
}
// ValidationEnum 枚举型
func ValidationEnum(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
if len(args) < 1 {
return true
}
for _, arg := range args {
if arg == value {
return true
}
}
return false
}
// ValidationPattern 正则匹配
func ValidationPattern(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
if len(args) < 1 {
return true
}
re, err := regexp.Compile(string(str.Of(args[0])))
if err != nil {
return false
}
return re.Match([]byte(str.Of(value)))
}
// ValidationMinLength 最小长度
func ValidationMinLength(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
if len(args) < 1 {
return true
}
return str.Of(value).Length() >= any.Of(args[0]).CInt()
}
// ValidationMaxLength 最大长度
func ValidationMaxLength(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
if len(args) < 1 {
return true
}
return str.Of(value).Length() <= any.Of(args[0]).CInt()
}
// ValidationEmail 验证邮箱
func ValidationEmail(value interface{}, _ maps.MapStrAny, _ ...interface{}) bool {
v := any.Of(value)
if !v.IsString() {
return false
}
_, err := mail.ParseAddress(v.String())
return err == nil
}
// ValidationMobile 验证手机号
func ValidationMobile(value interface{}, _ maps.MapStrAny, args ...interface{}) bool {
v := any.Of(value)
if !v.IsString() {
return false
}
zone := "cn"
if len(args) > 0 {
zone = any.Of(args[0]).String()
}
reg := regexp.MustCompile("^1[3-9]\\d{9}$")
switch zone {
case "us":
reg = regexp.MustCompile(`^[0-9]{3}-[0-9]{3}-[0-9]{4}$`)
}
return reg.MatchString(v.String())
}