-
Notifications
You must be signed in to change notification settings - Fork 34
/
validator.go
300 lines (250 loc) · 6.32 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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package validator
import (
"context"
"encoding"
"fmt"
"reflect"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator/rules"
"github.com/machinefi/w3bstream/pkg/depends/x/contextx"
"github.com/machinefi/w3bstream/pkg/depends/x/reflectx"
"github.com/machinefi/w3bstream/pkg/depends/x/textx"
"github.com/machinefi/w3bstream/pkg/depends/x/typesx"
)
// Validator can validate input value
type Validator interface {
Validate(interface{}) error
String() string
}
type CanValidate interface {
Validate() error
}
// Creator interface can new a validator
type Creator interface {
Names() []string
New(context.Context, *Rule) (Validator, error)
}
// Modifier can change rule's option
type Modifier interface {
SetOptional(opt bool)
SetDefaultValue(dft []byte)
SetErrMsg(err []byte)
}
type Processor = func(rule Modifier)
// Rule with internal error message and type identifier
type Rule struct {
*rules.Rule
ErrMsg []byte
Type typesx.Type
}
func ParseRuleByType(r []byte, t typesx.Type) (*Rule, error) {
rule := &rules.Rule{}
if len(r) != 0 {
parsed, err := rules.ParseRaw(r)
if err != nil {
return nil, err
}
rule = parsed
}
return &Rule{Rule: rule, Type: t}, nil
}
func ParseRuleStringByType(r string, t typesx.Type) (*Rule, error) {
return ParseRuleByType([]byte(r), t)
}
func MustParseRuleByType(r []byte, t typesx.Type) *Rule {
rule, err := ParseRuleByType(r, t)
if err != nil {
panic(err)
}
return rule
}
func MustParseRuleStringByType(r string, t typesx.Type) *Rule {
return MustParseRuleByType([]byte(r), t)
}
func (r *Rule) SetOptional(opt bool) { r.Optional = opt }
func (r *Rule) SetDefaultValue(dft []byte) { r.DftValue = dft }
func (r *Rule) SetErrMsg(msg []byte) { r.ErrMsg = msg }
func (r *Rule) String() string {
return typesx.FullTypeName(r.Type) + string(r.Rule.Bytes())
}
type Factory interface {
Compile(context.Context, []byte, typesx.Type, ...Processor) (Validator, error)
}
type ckCompiler struct{}
func ContextWithFactory(ctx context.Context, c Factory) context.Context {
return contextx.WithValue(ctx, ckCompiler{}, c)
}
func FactoryFromContext(ctx context.Context) Factory {
if f, ok := ctx.Value(ckCompiler{}).(Factory); ok {
return f
}
return DefaultFactory
}
type factory struct {
set map[string]Creator
}
var DefaultFactory = NewFactory()
func NewFactory() *factory { return &factory{set: make(map[string]Creator)} }
func (f *factory) Register(creators ...Creator) {
for i := range creators {
for _, name := range creators[i].Names() {
f.set[name] = creators[i]
}
}
}
func (f *factory) MustCompile(ctx context.Context, rule []byte, t typesx.Type, processors ...Processor) Validator {
v, err := f.Compile(ctx, rule, t, processors...)
if err != nil {
panic(err)
}
return v
}
func (f *factory) Compile(ctx context.Context, rule []byte, t typesx.Type, processors ...Processor) (Validator, error) {
if ctx == nil {
ctx = context.Background()
}
if len(rule) == 0 {
if _, ok := typesx.EncodingTextMarshalerTypeReplacer(t); !ok {
switch typesx.DeRef(t).Kind() {
case reflect.Struct:
rule = []byte("@struct")
case reflect.Slice:
rule = []byte("@slice")
case reflect.Map:
rule = []byte("@map")
}
}
}
r, err := ParseRuleByType(rule, t)
if err != nil {
return nil, err
}
for _, proc := range processors {
if proc != nil {
proc(r)
}
}
creator, ok := f.set[r.Name]
if !ok && len(rule) > 0 {
return nil, fmt.Errorf("%s not match any validator", r.Name)
}
return NewLoader(creator).New(ContextWithFactory(ctx, f), r)
}
type PreprocessStage int
const (
PreprocessSkip PreprocessStage = iota
PreprocessString
PreprocessPtr
)
// Loader load from creator
type Loader struct {
Creator Creator
Validator
PreprocessStage
DftValue []byte
Optional bool
ErrMsg []byte
}
func NewLoader(c Creator) *Loader { return &Loader{Creator: c} }
func normalize(typ typesx.Type) (typesx.Type, PreprocessStage) {
if t, ok := typesx.EncodingTextMarshalerTypeReplacer(typ); ok {
return t, PreprocessString
}
if typ.Kind() == reflect.Ptr {
return typesx.DeRef(typ), PreprocessPtr
}
return typ, PreprocessSkip
}
func (l *Loader) String() string {
if l.Validator != nil {
v := l.Validator.String()
if l.Optional {
if l.DftValue != nil {
return v + " = " + string(rules.SingleQuote(l.DftValue))
}
return v + "?"
}
return v
}
return "nil"
}
func (l *Loader) New(ctx context.Context, rule *Rule) (Validator, error) {
ret := NewLoader(l.Creator)
ret.Optional = rule.Optional
ret.DftValue = rule.DftValue
ret.ErrMsg = rule.ErrMsg
typ := rule.Type
rule.Type, ret.PreprocessStage = normalize(rule.Type)
if l.Creator != nil {
v, err := l.Creator.New(ctx, rule)
if err != nil {
return nil, err
}
ret.Validator = v
if ret.DftValue != nil {
if rv, ok := typesx.TryNew(typ); ok {
if err := textx.UnmarshalText(rv, ret.DftValue); err != nil {
return nil, fmt.Errorf(
"default value `%s` can not unmarshal to %s: %s",
ret.DftValue, typ, err,
)
}
if err := ret.Validate(rv); err != nil {
return nil, fmt.Errorf(
"default value `%s` is not a valid value of %s: %s",
ret.DftValue, v, err,
)
}
}
}
}
return ret, nil
}
func (l *Loader) Validate(v interface{}) error {
err := l.validate(v)
if err == nil {
return nil
}
if l.ErrMsg != nil && len(l.ErrMsg) != 0 {
return fmt.Errorf(string(l.ErrMsg))
}
return err
}
func (l *Loader) validate(v interface{}) error {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
if reflectx.IsEmptyValue(rv) {
if !l.Optional {
return errors.MissingRequiredFieldError{}
}
if l.DftValue != nil && rv.CanSet() {
if err := textx.UnmarshalText(rv, l.DftValue); err != nil {
return fmt.Errorf("unmarshal default value failed")
}
}
// empty value should not to validate
return nil
}
if l.Validator == nil {
return nil
}
if l.PreprocessStage == PreprocessString {
// make sure value over reflect.Value
if rv.CanInterface() {
v = rv.Interface()
}
if marshaller, ok := v.(encoding.TextMarshaler); ok {
data, err := marshaller.MarshalText()
if err != nil {
return err
}
return l.Validator.Validate(string(data))
}
}
if rv.Kind() == reflect.Interface {
rv = rv.Elem()
}
return l.Validator.Validate(reflectx.Indirect(rv))
}