-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator.go
163 lines (132 loc) · 3.63 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
package validator
import (
"context"
"fmt"
"reflect"
"github.com/go-courier/reflectx/typesutil"
"github.com/go-courier/validator/rules"
)
func MustParseRuleStringWithType(ruleStr string, typ typesutil.Type) *Rule {
r, err := ParseRuleWithType([]byte(ruleStr), typ)
if err != nil {
panic(err)
}
return r
}
func ParseRuleWithType(ruleBytes []byte, typ typesutil.Type) (*Rule, error) {
r := &rules.Rule{}
if len(ruleBytes) != 0 {
parsedRule, err := rules.ParseRule(ruleBytes)
if err != nil {
return nil, err
}
r = parsedRule
}
return &Rule{
Type: typ,
Rule: r,
}, nil
}
type Rule struct {
*rules.Rule
ErrMsg []byte
Type typesutil.Type
}
func (r *Rule) String() string {
return typesutil.FullTypeName(r.Type) + string(r.Rule.Bytes())
}
func (r *Rule) SetOptional(optional bool) {
r.Optional = optional
}
func (r *Rule) SetErrMsg(errMsg []byte) {
r.ErrMsg = errMsg
}
func (r *Rule) SetDefaultValue(defaultValue []byte) {
r.DefaultValue = defaultValue
}
type RuleModifier interface {
SetOptional(optional bool)
SetDefaultValue(defaultValue []byte)
SetErrMsg(errMsg []byte)
}
type RuleProcessor = func(rule RuleModifier)
// mgr for compiling validator
type ValidatorMgr interface {
// compile rule string to validator
Compile(context.Context, []byte, typesutil.Type, ...RuleProcessor) (Validator, error)
}
var ValidatorMgrDefault = NewValidatorFactory()
type contextKeyValidatorMgr int
func ContextWithValidatorMgr(c context.Context, validatorMgr ValidatorMgr) context.Context {
return context.WithValue(c, contextKeyValidatorMgr(1), validatorMgr)
}
func ValidatorMgrFromContext(c context.Context) ValidatorMgr {
return c.Value(contextKeyValidatorMgr(1)).(ValidatorMgr)
}
type ValidatorCreator interface {
// name and aliases of validator
// we will register validator to validator set by these names
Names() []string
// create new instance
New(context.Context, *Rule) (Validator, error)
}
type Validator interface {
// validate value
Validate(v interface{}) error
// stringify validator rule
String() string
}
func NewValidatorFactory() *ValidatorFactory {
return &ValidatorFactory{
validatorSet: map[string]ValidatorCreator{},
}
}
type ValidatorFactory struct {
validatorSet map[string]ValidatorCreator
}
func (f *ValidatorFactory) Register(validators ...ValidatorCreator) {
for i := range validators {
validator := validators[i]
for _, name := range validator.Names() {
f.validatorSet[name] = validator
}
}
}
func (f *ValidatorFactory) MustCompile(ctx context.Context, rule []byte, typ typesutil.Type, ruleProcessors ...RuleProcessor) Validator {
v, err := f.Compile(ctx, rule, typ, ruleProcessors...)
if err != nil {
panic(err)
}
return v
}
func (f *ValidatorFactory) Compile(ctx context.Context, ruleBytes []byte, typ typesutil.Type, ruleProcessors ...RuleProcessor) (Validator, error) {
if ctx == nil {
ctx = context.Background()
}
if len(ruleBytes) == 0 {
if _, ok := typesutil.EncodingTextMarshalerTypeReplacer(typ); !ok {
switch typesutil.Deref(typ).Kind() {
case reflect.Struct:
ruleBytes = []byte("@struct")
case reflect.Slice:
ruleBytes = []byte("@slice")
case reflect.Map:
ruleBytes = []byte("@map")
}
}
}
rule, err := ParseRuleWithType(ruleBytes, typ)
if err != nil {
return nil, err
}
for i := range ruleProcessors {
if ruleProcessor := ruleProcessors[i]; ruleProcessor != nil {
ruleProcessor(rule)
}
}
validatorCreator, ok := f.validatorSet[rule.Name]
if len(ruleBytes) != 0 && !ok {
return nil, fmt.Errorf("%s not match any validator", rule.Name)
}
return NewValidatorLoader(validatorCreator).New(ContextWithValidatorMgr(ctx, f), rule)
}