-
Notifications
You must be signed in to change notification settings - Fork 1
/
validator.go
213 lines (171 loc) · 4.78 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// +gengo:runtimedoc=false
package validator
import (
"context"
"fmt"
"reflect"
"github.com/octohelm/courier/pkg/validator/rules"
typesutil "github.com/octohelm/x/types"
)
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) SetOptional(optional bool) {
r.Optional = optional
}
func (r *Rule) SetErrMsg(errMsg []byte) {
r.ErrMsg = errMsg
}
func (r *Rule) SetDefaultValue(defaultValue []byte) {
r.DefaultValue = defaultValue
}
func (r *Rule) String() string {
return typesutil.FullTypeName(r.Type) + string(r.Rule.Bytes())
}
type RuleModifier interface {
SetOptional(optional bool)
SetDefaultValue(defaultValue []byte)
SetErrMsg(errMsg []byte)
}
type RuleProcessor = func(rule RuleModifier)
// Mgr mgr for compiling validator
type Mgr interface {
// Compile compile rule string to validator
Compile(context.Context, []byte, typesutil.Type, ...RuleProcessor) (Validator, error)
}
func MustCompile(ctx context.Context, b []byte, t typesutil.Type, ps ...RuleProcessor) Validator {
v, err := MgrFromContext(ctx).Compile(ctx, b, t, ps...)
if err != nil {
panic(err)
}
return v
}
func Compile(ctx context.Context, b []byte, t typesutil.Type, ps ...RuleProcessor) (Validator, error) {
return MgrFromContext(ctx).Compile(ctx, b, t, ps...)
}
func Register(validators ...Creator) {
mgrDefault.Register(validators...)
}
var mgrDefault = NewValidatorFactory()
type contextKeyMgr struct{}
func ContextWithMgr(c context.Context, validatorMgr Mgr) context.Context {
return context.WithValue(c, contextKeyMgr{}, validatorMgr)
}
func MgrFromContext(c context.Context) Mgr {
if mgr, ok := c.Value(contextKeyMgr{}).(Mgr); ok {
return mgr
}
return mgrDefault
}
type Creator interface {
// Names name and aliases of validator
// we will register validator to validator set by these names
Names() []string
// New create new instance
New(context.Context, *Rule) (Validator, error)
}
type Validator interface {
// Validate validate value
Validate(v interface{}) error
// String stringify validator rule
String() string
}
func NewValidatorFactory() *ValidatorFactory {
return &ValidatorFactory{
validatorSet: map[string]Creator{},
}
}
type ValidatorFactory struct {
validatorSet map[string]Creator
}
func (f *ValidatorFactory) Register(validators ...Creator) {
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 Validator, err error) {
if ctx == nil {
ctx = context.Background()
}
compiled := compiledFromContext(ctx)
// avoid for tree parse
if pkgPath := typesutil.Deref(typ).PkgPath(); pkgPath != "" {
id := pkgPath + "." + typ.Name()
if v, ok := compiled[id]; ok {
return v, nil
}
compiled[id] = validator
}
if len(ruleBytes) == 1 && ruleBytes[0] == '-' {
ruleBytes = nil
}
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(contextWithCompiled(ContextWithMgr(ctx, f), compiled), rule)
}
type contextKeyCompiled struct{}
func contextWithCompiled(ctx context.Context, compiled map[string]Validator) context.Context {
return context.WithValue(ctx, contextKeyCompiled{}, compiled)
}
func compiledFromContext(ctx context.Context) map[string]Validator {
if c, ok := ctx.Value(contextKeyCompiled{}).(map[string]Validator); ok {
return c
}
return map[string]Validator{}
}