Repository navigation
Expand file tree
/
Copy pathstring.go
More file actions
175 lines (127 loc) · 4.46 KB
/
Copy pathstring.go
File metadata and controls
175 lines (127 loc) · 4.46 KB
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
package validator
import (
"strings"
"unicode"
)
func init() {
registerRules(
&alphaRule{},
&alphanumRule{},
&asciiRule{},
&lowercaseRule{},
&uppercaseRule{},
&containsRule{},
&excludesRule{},
&startsWithRule{},
&endsWithRule{},
)
}
var (
_ Rule = (*alphaRule)(nil)
_ Rule = (*alphanumRule)(nil)
_ Rule = (*asciiRule)(nil)
_ Rule = (*lowercaseRule)(nil)
_ Rule = (*uppercaseRule)(nil)
_ Rule = (*containsRule)(nil)
_ Rule = (*excludesRule)(nil)
_ Rule = (*startsWithRule)(nil)
_ Rule = (*endsWithRule)(nil)
_ leafCompiler = (*alphaRule)(nil)
_ leafCompiler = (*alphanumRule)(nil)
_ leafCompiler = (*asciiRule)(nil)
_ leafCompiler = (*lowercaseRule)(nil)
_ leafCompiler = (*uppercaseRule)(nil)
)
type alphaRule struct{}
func (r *alphaRule) Signature() string { return "alpha" }
func (r *alphaRule) Passes(f *Field) bool {
return runesAllPass(f, unicode.IsLetter)
}
func (r *alphaRule) Message() string { return "The {field} field must only contain letters." }
func (r *alphaRule) compilePasses([]string) func(*Field) bool { return r.Passes }
type alphanumRule struct{}
func (r *alphanumRule) Signature() string { return "alphanum" }
func (r *alphanumRule) Passes(f *Field) bool {
return runesAllPass(f, func(c rune) bool { return unicode.IsLetter(c) || unicode.IsDigit(c) })
}
func (r *alphanumRule) Message() string {
return "The {field} field must only contain letters and numbers."
}
func (r *alphanumRule) compilePasses([]string) func(*Field) bool { return r.Passes }
type asciiRule struct{}
func (r *asciiRule) Signature() string { return "ascii" }
func (r *asciiRule) Passes(f *Field) bool {
return runesAllPass(f, func(c rune) bool { return c <= unicode.MaxASCII })
}
func (r *asciiRule) Message() string {
return "The {field} field must only contain single-byte alphanumeric characters and symbols."
}
func (r *asciiRule) compilePasses([]string) func(*Field) bool { return r.Passes }
type lowercaseRule struct{}
func (r *lowercaseRule) Signature() string { return "lowercase" }
func (r *lowercaseRule) Passes(f *Field) bool {
return caseMatch(f, strings.ToLower)
}
func (r *lowercaseRule) Message() string { return "The {field} field must be lowercase." }
func (r *lowercaseRule) compilePasses([]string) func(*Field) bool { return r.Passes }
type uppercaseRule struct{}
func (r *uppercaseRule) Signature() string { return "uppercase" }
func (r *uppercaseRule) Passes(f *Field) bool {
return caseMatch(f, strings.ToUpper)
}
func (r *uppercaseRule) Message() string { return "The {field} field must be uppercase." }
func (r *uppercaseRule) compilePasses([]string) func(*Field) bool { return r.Passes }
type containsRule struct{}
func (r *containsRule) Signature() string { return "contains" }
func (r *containsRule) Passes(f *Field) bool {
return substringMatch(f, strings.Contains)
}
func (r *containsRule) Message() string { return "The {field} field must contain {0}." }
type excludesRule struct{}
func (r *excludesRule) Signature() string { return "excludes" }
func (r *excludesRule) Passes(f *Field) bool {
return substringMatch(f, func(s, arg string) bool { return !strings.Contains(s, arg) })
}
func (r *excludesRule) Message() string { return "The {field} field must not contain {0}." }
type startsWithRule struct{}
func (r *startsWithRule) Signature() string { return "startswith" }
func (r *startsWithRule) Passes(f *Field) bool {
return substringMatch(f, strings.HasPrefix)
}
func (r *startsWithRule) Message() string { return "The {field} field must start with {0}." }
type endsWithRule struct{}
func (r *endsWithRule) Signature() string { return "endswith" }
func (r *endsWithRule) Passes(f *Field) bool {
return substringMatch(f, strings.HasSuffix)
}
func (r *endsWithRule) Message() string { return "The {field} field must end with {0}." }
func runesAllPass(f *Field, ok func(rune) bool) bool {
if isEmptyV(f.Reflect()) {
return true
}
for _, c := range valString(f.Reflect()) {
if !ok(c) {
return false
}
}
return true
}
// substringMatch passes an empty value and a missing argument (nothing to
// check), otherwise applies match to the string form.
func substringMatch(f *Field, match func(s, arg string) bool) bool {
if isEmptyV(f.Reflect()) {
return true
}
attrs := f.Attrs()
if len(attrs) == 0 {
return true
}
return match(valString(f.Reflect()), attrs[0])
}
func caseMatch(f *Field, transform func(string) string) bool {
if isEmptyV(f.Reflect()) {
return true
}
s := valString(f.Reflect())
return s == transform(s)
}