-
Notifications
You must be signed in to change notification settings - Fork 55
/
has.go
130 lines (115 loc) · 3.35 KB
/
has.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
package zvalid
import (
"strings"
"unicode"
)
// HasLetter must contain letters not case sensitive
func (v Engine) HasLetter(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
for _, rv := range v.value {
if unicode.IsLower(rv) || unicode.IsUpper(rv) {
return v
}
}
v.err = setError(v, "必须包含字母", customError...)
return v
})
}
// HasLower must contain lowercase letters
func (v Engine) HasLower(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
for _, rv := range v.value {
if unicode.IsLower(rv) {
return v
}
}
v.err = setError(v, "必须包含小写字母", customError...)
return v
})
}
// HasUpper must contain uppercase letters
func (v Engine) HasUpper(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
for _, rv := range v.value {
if unicode.IsUpper(rv) {
return v
}
}
v.err = setError(v, "必须包含大写字母", customError...)
return v
})
}
// HasNumber must contain numbers
func (v Engine) HasNumber(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
for _, rv := range v.value {
if unicode.IsDigit(rv) {
return v
}
}
v.err = setError(v, "必须包含数字", customError...)
return v
})
}
// HasSymbol must contain symbols
func (v Engine) HasSymbol(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
for _, rv := range v.value {
if !unicode.IsDigit(rv) && !unicode.IsLetter(rv) && !unicode.Is(unicode.Han, rv) {
return v
}
}
v.err = setError(v, "必须包含符号", customError...)
return v
})
}
// HasString must contain a specific string
func (v Engine) HasString(sub string, customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
if !strings.Contains(v.value, sub) {
v.err = setError(v, "必须包含特定的字符串", customError...)
return v
}
return v
}, true)
}
// HasPrefix must contain the specified prefix string
func (v Engine) HasPrefix(sub string, customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
if !strings.HasPrefix(v.value, sub) {
v.err = setError(v, "不允许的值", customError...)
return v
}
return v
})
}
// HasSuffix contains the specified suffix string
func (v Engine) HasSuffix(sub string, customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
if !strings.HasSuffix(v.value, sub) {
v.err = setError(v, "不允许的值", customError...)
return v
}
return v
})
}
// Password Universal password (any visible character, length between 6 ~ 20)
func (v Engine) Password(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
err := New().Verifi(v.value, v.name).Required().MinLength(6).MaxLength(20).Error()
if err != nil {
v.err = setError(v, "值不合法", customError...)
}
return v
})
}
// StrongPassword Strong equal strength password (length is 6 ~ 20, must include uppercase and lowercase letters, numbers and special characters)
func (v Engine) StrongPassword(customError ...string) Engine {
return pushQueue(&v, func(v *Engine) *Engine {
err := New().Verifi(v.value).Required().MinLength(6).MaxLength(20).HasSymbol().HasNumber().HasLetter().HasLower().Error()
if err != nil {
v.err = setError(v, "值不合法", customError...)
}
return v
})
}