-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.go
More file actions
350 lines (315 loc) · 9.26 KB
/
validation.go
File metadata and controls
350 lines (315 loc) · 9.26 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package applause
import (
"fmt"
"regexp"
"strconv"
"strings"
"time"
)
// Validator represents a validation function for argument values
type Validator interface {
Validate(value *Value) error
Description() string
}
// ValidatorFunc is a function type that implements the Validator interface
type ValidatorFunc struct {
validate func(*Value) error
description string
}
func (vf *ValidatorFunc) Validate(value *Value) error {
return vf.validate(value)
}
func (vf *ValidatorFunc) Description() string {
return vf.description
}
// NewValidator creates a new validator from a function and description
func NewValidator(validate func(*Value) error, description string) Validator {
return &ValidatorFunc{
validate: validate,
description: description,
}
}
// Built-in validators
// Range validators
// IntRange validates that an integer value is within the specified range (inclusive)
func IntRange(min, max int) Validator {
return NewValidator(func(value *Value) error {
intVal, err := value.Int()
if err != nil {
return err
}
if intVal < min || intVal > max {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("value must be between %d and %d", min, max),
Rule: fmt.Sprintf("range(%d,%d)", min, max),
}
}
return nil
}, fmt.Sprintf("must be between %d and %d", min, max))
}
// Float64Range validates that a float64 value is within the specified range (inclusive)
func Float64Range(min, max float64) Validator {
return NewValidator(func(value *Value) error {
floatVal, err := value.Float64()
if err != nil {
return err
}
if floatVal < min || floatVal > max {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("value must be between %g and %g", min, max),
Rule: fmt.Sprintf("range(%g,%g)", min, max),
}
}
return nil
}, fmt.Sprintf("must be between %g and %g", min, max))
}
// DurationRange validates that a duration value is within the specified range (inclusive)
func DurationRange(min, max time.Duration) Validator {
return NewValidator(func(value *Value) error {
durVal, err := value.Duration()
if err != nil {
return err
}
if durVal < min || durVal > max {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("duration must be between %v and %v", min, max),
Rule: fmt.Sprintf("duration_range(%v,%v)", min, max),
}
}
return nil
}, fmt.Sprintf("must be between %v and %v", min, max))
}
// Minimum/Maximum validators
// IntMin validates that an integer value is at least the specified minimum
func IntMin(min int) Validator {
return NewValidator(func(value *Value) error {
intVal, err := value.Int()
if err != nil {
return err
}
if intVal < min {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("value must be at least %d", min),
Rule: fmt.Sprintf("min(%d)", min),
}
}
return nil
}, fmt.Sprintf("must be at least %d", min))
}
// IntMax validates that an integer value is at most the specified maximum
func IntMax(max int) Validator {
return NewValidator(func(value *Value) error {
intVal, err := value.Int()
if err != nil {
return err
}
if intVal > max {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("value must be at most %d", max),
Rule: fmt.Sprintf("max(%d)", max),
}
}
return nil
}, fmt.Sprintf("must be at most %d", max))
}
// String validators
// StringLength validates that a string has the exact specified length
func StringLength(length int) Validator {
return NewValidator(func(value *Value) error {
strVal, err := value.String()
if err != nil {
return err
}
if len(strVal) != length {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("string must be exactly %d characters long", length),
Rule: fmt.Sprintf("length(%d)", length),
}
}
return nil
}, fmt.Sprintf("must be exactly %d characters long", length))
}
// StringMinLength validates that a string has at least the specified length
func StringMinLength(minLength int) Validator {
return NewValidator(func(value *Value) error {
strVal, err := value.String()
if err != nil {
return err
}
if len(strVal) < minLength {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("string must be at least %d characters long", minLength),
Rule: fmt.Sprintf("min_length(%d)", minLength),
}
}
return nil
}, fmt.Sprintf("must be at least %d characters long", minLength))
}
// StringMaxLength validates that a string has at most the specified length
func StringMaxLength(maxLength int) Validator {
return NewValidator(func(value *Value) error {
strVal, err := value.String()
if err != nil {
return err
}
if len(strVal) > maxLength {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("string must be at most %d characters long", maxLength),
Rule: fmt.Sprintf("max_length(%d)", maxLength),
}
}
return nil
}, fmt.Sprintf("must be at most %d characters long", maxLength))
}
// RegexMatch validates that a string matches the specified regular expression
func RegexMatch(pattern string) Validator {
re := regexp.MustCompile(pattern)
return NewValidator(func(value *Value) error {
strVal, err := value.String()
if err != nil {
return err
}
if !re.MatchString(strVal) {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("string must match pattern: %s", pattern),
Rule: fmt.Sprintf("regex(%s)", pattern),
}
}
return nil
}, fmt.Sprintf("must match pattern: %s", pattern))
}
// Choice validators
// StringChoice validates that a string value is one of the specified choices
func StringChoice(choices ...string) Validator {
choiceSet := make(map[string]bool)
for _, choice := range choices {
choiceSet[choice] = true
}
return NewValidator(func(value *Value) error {
strVal, err := value.String()
if err != nil {
return err
}
if !choiceSet[strVal] {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("value must be one of: %s", strings.Join(choices, ", ")),
Rule: fmt.Sprintf("choice(%s)", strings.Join(choices, ",")),
}
}
return nil
}, fmt.Sprintf("must be one of: %s", strings.Join(choices, ", ")))
}
// IntChoice validates that an integer value is one of the specified choices
func IntChoice(choices ...int) Validator {
choiceSet := make(map[int]bool)
choiceStrs := make([]string, len(choices))
for i, choice := range choices {
choiceSet[choice] = true
choiceStrs[i] = strconv.Itoa(choice)
}
return NewValidator(func(value *Value) error {
intVal, err := value.Int()
if err != nil {
return err
}
if !choiceSet[intVal] {
return &ValidationError{
Value: value.Raw(),
Message: fmt.Sprintf("value must be one of: %s", strings.Join(choiceStrs, ", ")),
Rule: fmt.Sprintf("choice(%s)", strings.Join(choiceStrs, ",")),
}
}
return nil
}, fmt.Sprintf("must be one of: %s", strings.Join(choiceStrs, ", ")))
}
// Composite validators
// All validates that all of the provided validators pass
func All(validators ...Validator) Validator {
descriptions := make([]string, len(validators))
for i, v := range validators {
descriptions[i] = v.Description()
}
return NewValidator(func(value *Value) error {
for _, validator := range validators {
if err := validator.Validate(value); err != nil {
return err
}
}
return nil
}, strings.Join(descriptions, " and "))
}
// Any validates that at least one of the provided validators passes
func Any(validators ...Validator) Validator {
descriptions := make([]string, len(validators))
for i, v := range validators {
descriptions[i] = v.Description()
}
return NewValidator(func(value *Value) error {
var lastErr error
for _, validator := range validators {
if err := validator.Validate(value); err == nil {
return nil // At least one passed
} else {
lastErr = err
}
}
// None passed, return the last error
return lastErr
}, fmt.Sprintf("must satisfy one of: %s", strings.Join(descriptions, " or ")))
}
// File/Path validators
// FileExists validates that a file exists at the given path
func FileExists() Validator {
return NewValidator(func(value *Value) error {
strVal, err := value.String()
if err != nil {
return err
}
// This is a simplified check - in a real implementation you'd use os.Stat
if strVal == "" {
return &ValidationError{
Value: value.Raw(),
Message: "file path cannot be empty",
Rule: "file_exists",
}
}
return nil
}, "file must exist")
}
// Extension for adding validators to arguments
// AddValidator adds a validator to an argument
func (a *Arg) AddValidator(validator Validator) *Arg {
if a.validators == nil {
a.validators = make([]Validator, 0)
}
a.validators = append(a.validators, validator)
return a
}
// GetValidators returns all validators for this argument
func (a *Arg) GetValidators() []Validator {
return a.validators
}
// Validate runs all validators against a value
func (a *Arg) Validate(value *Value) []error {
var errors []error
for _, validator := range a.validators {
if err := validator.Validate(value); err != nil {
// Enhance error with argument context
if valErr, ok := err.(*ValidationError); ok {
valErr.Argument = a.name
}
errors = append(errors, err)
}
}
return errors
}