-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_string.go
212 lines (207 loc) · 5.53 KB
/
spec_string.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package vd
import (
"github.com/hoisie/mustache"
"log"
"regexp"
"runtime/debug"
)
type StringSpec struct {
Name string
Path string
AllowEmpty bool
RuneLen uint64
RuneLenMessage string
MinRuneLen uint64
MinRuneLenMessage string
MaxRuneLen uint64
MaxRuneLenMessage string
Pattern []string
BanPattern []string
PatternMessage string
Enum []string
Ext []StringSpec
}
func (s StringSpec) NameIs(name string) StringSpec {
s.Name = name
return s
}
type stringSpecRender struct {
Value interface{}
StringSpec
}
func (spec StringSpec) render (message string, value interface{}) string {
context := stringSpecRender{
Value: value,
StringSpec: spec,
}
return mustache.Render(message, context)
}
func (r *Rule) String(v string, spec StringSpec) {
if r.Fail { return }
if v == "" {
if spec.AllowEmpty {
return
} else {
r.Break(r.Format.StringNotAllowEmpty(spec.Name, spec.Path), spec.Path)
return
}
}
if spec.CheckRuneLen(v, r) { return }
if spec.CheckMinRuneLen(v, r) { return }
if spec.CheckMaxRuneLen(v, r) { return }
if spec.CheckPattern (v, r) { return }
if spec.CheckBanPattern(v, r) { return }
if spec.CheckEnum(v, r) {return}
for _, ext := range spec.Ext {
ext.Name = spec.Name
ext.AllowEmpty = spec.AllowEmpty
if ext.PatternMessage == "" {
ext.PatternMessage = spec.PatternMessage
}
r.String(v, ext)
if r.Fail {return}
}
}
func (spec StringSpec) CheckRuneLen(v string, r *Rule) (fail bool) {
if spec.RuneLen == 0 {
return false
}
length := len([]rune(v))
pass := uint64(length) == spec.RuneLen
if !pass {
message := r.CreateMessage(spec.RuneLenMessage, func() string {
return r.Format.StringRuneLen(spec.Name, spec.Path, v, spec.RuneLen)
})
r.Break(spec.render(message, v), spec.Path)
}
return r.Fail
}
func (spec StringSpec) CheckMaxRuneLen(v string, r *Rule) (fail bool) {
if spec.MaxRuneLen == 0 {
return false
}
length := len([]rune(v))
pass := uint64(length) <= spec.MaxRuneLen
if !pass {
message := r.CreateMessage(spec.MaxRuneLenMessage, func() string {
return r.Format.StringMaxRuneLen(spec.Name, spec.Path, v, spec.MaxRuneLen)
})
r.Break(spec.render(message, v), spec.Path)
}
return r.Fail
}
func (spec StringSpec) CheckMinRuneLen(v string, r *Rule) (fail bool) {
length := len([]rune(v))
pass := uint64(length) >= spec.MinRuneLen
if !pass {
message := r.CreateMessage(spec.MinRuneLenMessage, func() string {
return r.Format.StringMinRuneLen(spec.Name, spec.Path, v, spec.MinRuneLen)
})
r.Break(spec.render(message, v), spec.Path)
}
return r.Fail
}
type patternData struct {
Pattern []string
PatternMessage string
Name string
Path string
}
func checkPattern(data patternData, render func(string, interface{}) string, v string, r *Rule, path string) (fail bool) {
if len(data.Pattern) == 0 {
return false
}
for _, pattern := range data.Pattern {
matched, err := regexp.MatchString(pattern, v) ; if err != nil {
// 此处如果将错误向上传递则会导致接口易用性下降
// panic 会导致不必要的中断服务
// 所以这里采取打印错误并验证错误的处理 @nimoc 2021年02月12日01:20:45 (新年快乐)
log.Print(err)
debug.PrintStack()
r.Break("goclub/validator: regexp pattern error(" + pattern + ")", path)
return
}
pass := matched
if !pass {
message := r.CreateMessage(data.PatternMessage, func() string {
return r.Format.Pattern(data.Name, data.Path, v, data.Pattern, pattern)
})
r.Break(render(message, v), path)
break
}
}
return r.Fail
}
func (spec StringSpec) CheckPattern(v string, r *Rule) (fail bool) {
return checkPattern(patternData{
Pattern: spec.Pattern,
PatternMessage: spec.PatternMessage,
Name: spec.Name,
Path: spec.Path,
}, spec.render, v, r, spec.Path)
}
type banPatternData struct {
BanPattern []string
PatternMessage string
Name string
Path string
}
func checkBanPattern(data banPatternData, render func(string, interface{}) string, v string, r *Rule, path string) (fail bool) {
if len(data.BanPattern) == 0 {
return false
}
for _, pattern := range data.BanPattern {
matched, err := regexp.MatchString(pattern, v) ; if err != nil {
// 此处如果将错误向上传递则会导致接口易用性下降
// panic 会导致不必要的中断服务
// 所以这里采取打印错误并验证错误的处理 @nimoc 2021年02月12日01:20:45 (新年快乐)
log.Print(err)
debug.PrintStack()
r.Break("goclub/validator: regexp ban pattern error(" + pattern + ")", path)
}
pass := !matched
if !pass {
message := r.CreateMessage(data.PatternMessage, func() string {
return r.Format.BanPattern(data.Name, data.Path, v, data.BanPattern, pattern)
})
r.Break(render(message, v), path)
break
}
}
return
}
func (spec StringSpec) CheckBanPattern(v string, r *Rule) (fail bool) {
return checkBanPattern(banPatternData{
BanPattern: spec.BanPattern,
PatternMessage: spec.PatternMessage,
Name: spec.Name,
Path: spec.Path,
}, spec.render, v, r, spec.Path)
}
func (spec StringSpec) CheckEnum(v string, r *Rule) (fail bool) {
if len(spec.Enum) == 0 {
return false
}
pass := false
for _, enum := range spec.Enum {
if enum == v {
pass = true
}
}
if !pass {
message := r.Format.StringEnum(spec.Name, spec.Path, v, spec.Enum)
r.Break(spec.render(message, v), spec.Path)
}
return r.Fail
}
func uniqueStrings(strings []string) (isRepeat bool, repeatElement string) {
hash := map[string]bool{}
for _, s := range strings {
_,ok := hash[s]; if ok {
return true, s
} else {
hash[s] = true
}
}
return
}