-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.go
79 lines (63 loc) · 1.62 KB
/
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
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
package validate
import (
"fmt"
"reflect"
"regexp"
"strings"
)
const validateTagName = "validate"
type validateOptions struct {
Required bool
Regex string
}
func Validate(obj interface{}) error {
t := reflect.TypeOf(obj)
v := reflect.ValueOf(obj)
for i := 0; i < t.NumField(); i++ {
fieldType := t.Field(i)
fieldValue := v.Field(i)
if validateValue, ok := fieldType.Tag.Lookup(validateTagName); ok {
options, err := parseValidateOptions(validateValue)
if err != nil {
return fmt.Errorf("Failed to parseValidateOptions:%w", err)
}
if err := validate(fieldValue, fieldType, options); err != nil {
return err
}
}
}
return nil
}
func parseValidateOptions(tagVal string) (validateOptions, error) {
var options validateOptions
values := strings.Split(tagVal, ",")
for _, v := range values {
kv := strings.Split(v, "=")
switch kv[0] {
case "required":
options.Required = true
case "regex":
if len(kv) != 2 {
return options, fmt.Errorf("parseValidOptions: regex option did not have an expression")
}
options.Regex = kv[1]
}
}
return options, nil
}
func validate(field reflect.Value, ftype reflect.StructField, options validateOptions) error {
strVal := fmt.Sprintf("%v", field.Interface())
if options.Required && strVal == "" {
return fmt.Errorf("required field '%s' did not have a value", ftype.Name)
}
if options.Regex != "" {
matched, err := regexp.MatchString(options.Regex, strVal)
if err != nil {
return err
}
if !matched {
return fmt.Errorf("field '%s' failed to pass regex '%s': '%s'", ftype.Name, options.Regex, strVal)
}
}
return nil
}