forked from connor4312/validity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.go
58 lines (50 loc) · 1.92 KB
/
checker.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
package validity
import (
"reflect"
"strings"
)
// ValidityChecker is the base interface from which type validators must implement.
type ValidityChecker interface {
// GetErrors is the method which is actually called to run validation. It should return a slice of validation rules
// which are invalid, or nil if there are no invalid rules.
GetItem() interface{}
GetKey() string
GetRules() []string
GetErrors() []string
}
// GetErrors runs the validation! What it does is, for each validation rule in the format "rule:arg1,arg2". Surrounding
// spaces will be trimmed out. It attempts to call a function defined like:
//
// func ValidateRule(arg1 string, arg2 string) bool { ... }
//
// It must return a boolean value (true if validation passed, false if it did not) and take string arguments.
func GetCheckerErrors(rules []string, instance ValidityChecker) []string {
errors := []string{}
for _, rule := range rules {
//omit rules used by parsers
if inSlice(rule, []string{"required"}) {
continue
}
// First we want to split the rule into its method and arguments parts,
// so we have a []string{"rule", "arg1,arg2"}.
parts := strings.SplitN(rule, ":", 2)
method := snakeToStudly(strings.ToLower(parts[0]))
// The parameters to call is a list of reflection values.
params := []reflect.Value{}
// If we do have arguments (some rules do not require them), the split the arguments part by commas and
// put their Values into the params array.
if len(parts) > 1 {
for _, arg := range strings.Split(parts[1], ",") {
params = append(params, reflect.ValueOf(strings.Trim(arg, " ")))
}
}
// Finall, call the validator...
valid := reflect.ValueOf(instance).MethodByName("Validate" + method).Call(params)[0].Bool()
// and if it is not valid, then we need to store it in the errors...
if !valid {
errors = append(errors, method)
}
}
// And finally return any errors which occured.
return errors
}