-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
168 lines (147 loc) · 3.69 KB
/
validator.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
package govalidator
import (
"errors"
"fmt"
"github.com/AndrewDanilin/govalidator/validators"
"reflect"
"strconv"
"strings"
)
var ErrNotStruct = errors.New("wrong argument given, should be a struct")
var ErrInvalidValidatorSyntax = errors.New("invalid validator syntax")
var ErrValidateForUnexportedFields = errors.New("validation for unexported field is not allowed")
var ErrUnsupportedType = errors.New("unsupported type")
type ValidationError struct {
Err error
}
type ValidationErrors []ValidationError
func (v ValidationErrors) Error() string {
if len(v) == 1 {
return strings.Split(v[0].Err.Error(), "err: ")[1]
}
arr := make([]string, len(v))
for i, valErr := range v {
arr[i] = valErr.Err.Error()
}
return strings.Join(arr, "\n")
}
func Validate(v any) error {
errs := ValidationErrors{}
vType := reflect.TypeOf(v)
vVal := reflect.ValueOf(v)
if vType.Kind() != reflect.Struct {
return ErrNotStruct
}
for i := 0; i < vType.NumField(); i++ {
f := vType.Field(i)
if vStr, ok := f.Tag.Lookup("validate"); ok {
if !f.IsExported() {
errs = append(errs, buildErr(f.Name, ErrValidateForUnexportedFields))
continue
}
var vrs []validators.Validator
var err error
switch f.Type.Kind() {
case reflect.String:
vrs, err = parseValidators(reflect.String, vStr)
case reflect.Int:
vrs, err = parseValidators(reflect.Int, vStr)
case reflect.Slice:
vrs, err = parseValidators(f.Type.Elem().Kind(), vStr)
default:
errs = append(errs, buildErr(f.Name, ErrUnsupportedType))
continue
}
if err != nil {
errs = append(errs, buildErr(f.Name, err))
continue
}
for _, vr := range vrs {
err = vr.Validate(vVal.Field(i).Interface())
if err != nil {
errs = append(errs, buildErr(f.Name, err))
}
}
}
}
if len(errs) == 0 {
return nil
}
return errs
}
func buildErr(name string, err error) ValidationError {
return ValidationError{Err: fmt.Errorf("name: %s, err: %s", name, err)}
}
func parseValidators(t reflect.Kind, vStr string) ([]validators.Validator, error) {
parts := strings.Split(vStr, ";")
vrs := make([]validators.Validator, len(parts))
for i, v := range parts {
vr, err := parseValidator(t, v)
if err != nil {
return nil, err
}
vrs[i] = vr
}
return vrs, nil
}
func parseValidator(
t reflect.Kind,
vStr string,
) (validators.Validator, error) {
parts := strings.Split(vStr, ":")
switch parts[0] {
case "max":
arg, err := strconv.Atoi(parts[1])
if err != nil {
return nil, ErrInvalidValidatorSyntax
}
return validators.MaxValidator{Arg: arg}, nil
case "min":
arg, err := strconv.Atoi(parts[1])
if err != nil {
return nil, ErrInvalidValidatorSyntax
}
return validators.MinValidator{Arg: arg}, nil
case "len":
arg, err := strconv.Atoi(parts[1])
if err != nil {
return nil, ErrInvalidValidatorSyntax
}
switch t {
case reflect.String:
return validators.LenValidator{Arg: arg}, nil
default:
return nil, ErrUnsupportedType
}
case "in":
args := strings.Split(parts[1], ",")
if len(args) == 1 && args[0] == parts[1] {
args = make([]string, 0)
}
switch t {
case reflect.String:
return validators.InValidator[string]{Args: args}, nil
case reflect.Int:
argsTyped := make([]int, len(args))
for i := range args {
n, err := strconv.Atoi(args[i])
if err != nil {
return nil, ErrInvalidValidatorSyntax
}
argsTyped[i] = n
}
return validators.InValidator[int]{Args: argsTyped}, nil
default:
}
case "not_empty":
switch t {
case reflect.String:
return validators.NotEmptyValidator{}, nil
default:
return nil, ErrUnsupportedType
}
default:
return nil, ErrInvalidValidatorSyntax
}
return nil, ErrInvalidValidatorSyntax
}