-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidation.go
138 lines (119 loc) · 3.65 KB
/
validation.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
// Package validation provides configurable and extensible rules for validating data of various types.
package validation
import (
"fmt"
"reflect"
"strconv"
"github.com/cadyrov/goerr/v2"
"github.com/cadyrov/govalidation/verror"
)
type (
// Validatable is the interface indicating the type implementing it supports data validation.
Validatable interface {
// Validate validates the data and returns an error if validation fails.
Validate() (code int, args []interface{})
}
// Rule represents a validation rule.
Rule interface {
// Validate validates a value and returns a value if validation fails.
Validate(value interface{}) (code int, args []interface{})
}
// RuleFunc represents a validator function.
// You may wrap it as a Rule by calling By().
RuleFunc func(value interface{}) (code int, args []interface{})
)
var (
// ErrorTag is the struct tag name used to customize the error field name for a struct field.
ErrorTag = "json"
// Skip is a special validation rule that indicates all rules following it should be skipped.
Skip = &skipRule{}
validatableType = reflect.TypeOf((*Validatable)(nil)).Elem()
)
// Validate validates the given value and returns the validation error, if any.
//
// Validate performs validation using the following steps:
// - validate the value against the rules passed in as parameters
// - if the value is a map and the map values implement `Validatable`, call `Validate` of every map value
// - if the value is a slice or array whose values implement `Validatable`, call `Validate` of every element
func Validate(value interface{}, rules ...Rule) goerr.IError {
for _, rule := range rules {
if _, ok := rule.(*skipRule); ok {
return nil
}
if code, args := rule.Validate(value); code != 0 {
return verror.NewGoErr(code, args...)
}
}
rv := reflect.ValueOf(value)
if (rv.Kind() == reflect.Ptr || rv.Kind() == reflect.Interface) && rv.IsNil() {
return nil
}
if v, ok := value.(Validatable); ok {
if code, args := v.Validate(); code != 0 {
return verror.NewGoErr(code, args...)
}
return nil
}
switch rv.Kind() {
case reflect.Map:
if rv.Type().Elem().Implements(validatableType) {
return validateMap(rv)
}
case reflect.Slice, reflect.Array:
if rv.Type().Elem().Implements(validatableType) {
return validateSlice(rv)
}
case reflect.Ptr, reflect.Interface:
return Validate(rv.Elem().Interface())
}
return nil
}
// validateMap validates a map of validatable elements
func validateMap(rv reflect.Value) goerr.IError {
errs := verror.NewErrStack("validationError")
for _, key := range rv.MapKeys() {
if mv := rv.MapIndex(key).Interface(); mv != nil {
if code, args := mv.(Validatable).Validate(); code != 0 {
e := verror.NewGoErr(code, args)
e.Tag(fmt.Sprintf("%v", key.Interface()))
errs.PushDetail(e)
}
}
}
if len(errs.Details()) > 0 {
return errs
}
return nil
}
// validateMap validates a slice/array of validatable elements
func validateSlice(rv reflect.Value) goerr.IError {
errs := verror.NewErrStack("validationError")
l := rv.Len()
for i := 0; i < l; i++ {
if ev := rv.Index(i).Interface(); ev != nil {
if code, args := ev.(Validatable).Validate(); code != 0 {
e := verror.NewGoErr(code, args)
e.Tag(strconv.Itoa(i))
errs.PushDetail(e)
}
}
}
if len(errs.Details()) > 0 {
return errs
}
return nil
}
type skipRule struct{}
func (r *skipRule) Validate(interface{}) (code int, args []interface{}) {
return 0, nil
}
type inlineRule struct {
f RuleFunc
}
func (r *inlineRule) Validate(value interface{}) (code int, args []interface{}) {
return r.f(value)
}
// By wraps a RuleFunc into a Rule.
func By(f RuleFunc) Rule {
return &inlineRule{f}
}