-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidators.go
61 lines (45 loc) · 1.11 KB
/
validators.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
package wtforms
import (
"regexp"
)
const RE_RFC_EMAIL = `(?i)[A-Z0-9!#$%&'*+/=?^_{|}~-]+` +
`(?:\.[A-Z0-9!#$%&'*+/=?^_{|}~-]+)*` +
`@(?:[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?\.)+` +
`[A-Z0-9](?:[A-Z0-9-]*[A-Z0-9])?`
type IValidator interface {
CleanData(value string) (bool, string)
}
type Required struct {
}
func (v Required) CleanData(value string) (bool, string) {
if value == "" {
return false, "该字段不能为空"
}
return true, ""
}
type Regexp struct {
Expr string
Message string
}
func (v Regexp) CleanData(value string) (bool, string) {
reg, err := regexp.Compile(v.Expr)
if err != nil {
panic(err)
}
if reg.MatchString(value) {
return true, ""
}
return false, v.Message
}
type Email struct {
}
func (v Email) CleanData(value string) (bool, string) {
tmp := Regexp{Expr: RE_RFC_EMAIL, Message: "无效的电子邮件地址"}
return tmp.CleanData(value)
}
type URL struct {
}
func (v URL) CleanData(value string) (bool, string) {
tmp := Regexp{Expr: `^(http|https)?://([^/:]+|([0-9]{1,3}\.){3}[0-9]{1,3})(:[0-9]+)?(\/.*)?$`, Message: "无效的URL"}
return tmp.CleanData(value)
}