-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.go
301 lines (272 loc) · 7.51 KB
/
functions.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package validator
import (
"encoding/hex"
"errors"
"fmt"
"net/mail"
"reflect"
"regexp"
"strconv"
"strings"
"time"
"golang.org/x/exp/constraints"
)
var (
reUKPostCode = regexp.MustCompile(`^[a-zA-Z]{1,2}\d[a-zA-Z\d]?\s*\d[a-zA-Z]{2}$`)
reZipCode = regexp.MustCompile(`^(\d{5}(?:\-\d{4})?)$`)
)
const (
validateEmpty = "value cannot be empty"
validateNotEmpty = "value must be empty"
validateLength = "value must be between %d and %d characters"
validateExactLength = "value should be exactly %d characters"
validateMin = "value %v is smaller than minimum %v"
validateMax = "value %v is larger than maximum %v"
validateNumBetween = "value %v must be between %v and %v"
validatePositive = "value %v should be greater than 0"
validateRegex = "value %s failed to meet requirements"
validateBool = "value %v does not evaluate to %v"
validateDateEqual = "the date/time provided %s, does not match the expected %s"
validateDateAfter = "the date provided %s, must be after %s"
validateDateBefore = "the date provided %s, must be before %s"
validateUkPostCode = "%s is not a valid UK PostCode"
validateIsNumeric = "string %s is not a number"
validateEmail = "invalid email"
)
// StrLength will ensure a string, val, has a length that is at least min and
// at most max.
func StrLength(val string, min, max int) ValidationFunc {
return func() error {
if len(val) >= min && len(val) <= max {
return nil
}
return fmt.Errorf(validateLength, min, max)
}
}
// StrLengthExact will ensure a string, val, is exactly length.
func StrLengthExact(val string, length int) ValidationFunc {
return func() error {
if len(val) == length {
return nil
}
return fmt.Errorf(validateExactLength, length)
}
}
// Number defines all number types.
type Number interface {
constraints.Integer | constraints.Float
}
// MinNumber will ensure a Number, val, is at least min in value.
func MinNumber[T Number](val, min T) ValidationFunc {
return func() error {
if val >= min {
return nil
}
return fmt.Errorf(validateMin, val, min)
}
}
// MaxNumber will ensure an Int, val, is at most Max in value.
func MaxNumber[T Number](val, max T) ValidationFunc {
return func() error {
if val <= max {
return nil
}
return fmt.Errorf(validateMax, val, max)
}
}
// BetweenNumber will ensure an int, val, is at least min and at most max.
func BetweenNumber[T Number](val, min, max T) ValidationFunc {
return func() error {
if val >= min && val <= max {
return nil
}
return fmt.Errorf(validateNumBetween, val, min, max)
}
}
// PositiveNumber will ensure an int, val, is > 0.
func PositiveNumber[T Number](val T) ValidationFunc {
return func() error {
if val > 0 {
return nil
}
return fmt.Errorf(validatePositive, val)
}
}
// MatchString will check that a string, val, matches the provided regular expression.
func MatchString(val string, r *regexp.Regexp) ValidationFunc {
return func() error {
if r.MatchString(val) {
return nil
}
return fmt.Errorf(validateRegex, val)
}
}
// MatchBytes will check that a byte array, val, matches the provided regular expression.
func MatchBytes(val []byte, r *regexp.Regexp) ValidationFunc {
return func() error {
if r.Match(val) {
return nil
}
return fmt.Errorf(validateRegex, val)
}
}
// Equal is a simple check to ensure that val matches exp.
func Equal[T comparable](val, exp T) ValidationFunc {
return func() error {
if val == exp {
return nil
}
return fmt.Errorf(validateBool, val, exp)
}
}
// DateEqual will ensure that a date/time, val, matches exactly exp.
func DateEqual(val, exp time.Time) ValidationFunc {
return func() error {
if val.Equal(exp) {
return nil
}
return fmt.Errorf(validateDateEqual, val, exp)
}
}
// DateAfter will ensure that a date/time, val, occurs after exp.
func DateAfter(val, exp time.Time) ValidationFunc {
return func() error {
if val.After(exp) {
return nil
}
return fmt.Errorf(validateDateAfter, val, exp)
}
}
// DateBefore will ensure that a date/time, val, occurs before exp.
func DateBefore(val, exp time.Time) ValidationFunc {
return func() error {
if val.Before(exp) {
return nil
}
return fmt.Errorf(validateDateBefore, val, exp)
}
}
// NotEmpty will ensure that a value, val, is not empty.
// rules are:
// int: > 0
// string: != "" or whitespace
// slice: not nil and len > 0
// map: not nil and len > 0
func NotEmpty(v interface{}) ValidationFunc {
return func() error {
if v == nil {
return fmt.Errorf(validateEmpty)
}
val := reflect.ValueOf(v)
valid := false
//nolint:exhaustive // not supporting everything
switch val.Kind() {
case reflect.Map, reflect.Slice:
valid = val.Len() > 0 && !val.IsNil()
default:
valid = !val.IsZero()
}
if !valid {
return fmt.Errorf(validateEmpty)
}
return nil
}
}
// Empty will ensure that a value, val, is empty.
// rules are:
// int: == 0
// string: == "" or whitespace
// slice: is nil or len == 0
// map: is nil and len == 0
func Empty(v interface{}) ValidationFunc {
return func() error {
err := NotEmpty(v)()
if err == nil {
return fmt.Errorf(validateNotEmpty)
}
return nil
}
}
// IsNumeric will pass if a string, val, is an Int.
func IsNumeric(val string) ValidationFunc {
return func() error {
_, err := strconv.Atoi(val)
if err == nil {
return nil
}
return fmt.Errorf(validateIsNumeric, val)
}
}
// UKPostCode will validate that a string, val, is a valid UK PostCode.
// It does not check the postcode exists, just that it matches an agreed pattern.
func UKPostCode(val string) ValidationFunc {
return func() error {
if reUKPostCode.MatchString(val) {
return nil
}
return fmt.Errorf(validateUkPostCode, val)
}
}
// USZipCode will validate that a string, val, matches a US USZipCode pattern.
// It does not check the zipcode exists, just that it matches an agreed pattern.
func USZipCode(val string) ValidationFunc {
return func() error {
if reZipCode.MatchString(val) {
return nil
}
return fmt.Errorf("%s is not a valid UK PostCode", val)
}
}
// HasPrefix ensures string, val, has a prefix matching prefix.
func HasPrefix(val, prefix string) ValidationFunc {
return func() error {
if strings.HasPrefix(val, prefix) {
return nil
}
return fmt.Errorf("value provided does not have a valid prefix")
}
}
// NoPrefix ensures a string, val, does not have the supplied prefix.
func NoPrefix(val, prefix string) ValidationFunc {
return func() error {
if strings.HasPrefix(val, prefix) {
return errors.New("value provided does not have a valid prefix")
}
return nil
}
}
// IsHex will check that a string, val, is valid Hexadecimal.
func IsHex(val string) ValidationFunc {
return func() error {
if _, err := hex.DecodeString(val); err != nil {
return errors.New("value supplied is not valid hex")
}
return nil
}
}
// Email will check that a string is a valid email address.
func Email(val string) ValidationFunc {
return func() error {
if _, err := mail.ParseAddress(val); err != nil {
return errors.New(validateEmail)
}
return nil
}
}
// Any will check if the provided value is in a set of allowed values.
func Any[T comparable](val T, vv ...T) ValidationFunc {
return func() error {
for _, v := range vv {
if val == v {
return nil
}
}
return errors.New("value not found in allowed values")
}
}
// AnyString will check if the provided string is in a set of allowed values.
//
// Deprecated: use Any instead. Will be removed in a future release.
func AnyString(val string, vv ...string) ValidationFunc {
return Any(val, vv...)
}