-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
44 lines (38 loc) · 850 Bytes
/
validate.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
package validators
// Applies all given validators to the passed value.
//
// Configuration parameters:
// - value (any): value to be validated.
// - validations ([]Validator): validators.
//
// Usage examples:
//
// validations := v.Validate(
// v.IsInt(),
// v.Min(3),
// v.Max(10),
// )
//
// value := 4
// validations(value) // Output: nil
//
// value = 2
// validations(value) // Output: [ error messages ]
func Validate(
validations ...Validator,
) func(value interface{}) *[]string {
var errorMessages []string
return func(value interface{}) *[]string {
errorMessages = []string{}
for _, validation := range validations {
errorMessage, stopLoop = validation(value)
if errorMessage != nil {
errorMessages = append(errorMessages, *errorMessage)
}
if stopLoop {
break
}
}
return &errorMessages
}
}