-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalidator_loader.go
155 lines (128 loc) · 3.29 KB
/
validator_loader.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
package validator
import (
"context"
"encoding"
stderrors "errors"
"fmt"
"reflect"
"github.com/go-courier/reflectx"
"github.com/go-courier/reflectx/typesutil"
"github.com/go-courier/validator/errors"
"github.com/go-courier/validator/rules"
)
func NewValidatorLoader(validatorCreator ValidatorCreator) *ValidatorLoader {
return &ValidatorLoader{
ValidatorCreator: validatorCreator,
}
}
type ValidatorLoader struct {
ValidatorCreator ValidatorCreator
Validator
PreprocessStage
DefaultValue []byte
Optional bool
ErrMsg []byte
}
type PreprocessStage int
const (
PreprocessSkip PreprocessStage = iota
PreprocessString
PreprocessPtr
)
func normalize(typ typesutil.Type) (typesutil.Type, PreprocessStage) {
if t, ok := typesutil.EncodingTextMarshalerTypeReplacer(typ); ok {
return t, PreprocessString
}
if typ.Kind() == reflect.Ptr {
return typesutil.Deref(typ), PreprocessPtr
}
return typ, PreprocessSkip
}
func (loader *ValidatorLoader) String() string {
if loader.Validator != nil {
v := loader.Validator.String()
if loader.Optional {
if loader.DefaultValue != nil {
return v + " = " + string(rules.SingleQuote(loader.DefaultValue))
}
return v + "?"
}
return v
}
return "nil"
}
func (loader *ValidatorLoader) New(ctx context.Context, rule *Rule) (Validator, error) {
l := NewValidatorLoader(loader.ValidatorCreator)
l.Optional = rule.Optional
l.DefaultValue = rule.DefaultValue
l.ErrMsg = rule.ErrMsg
typ := rule.Type
rule.Type, l.PreprocessStage = normalize(rule.Type)
if loader.ValidatorCreator != nil {
v, err := loader.ValidatorCreator.New(ctx, rule)
if err != nil {
return nil, err
}
l.Validator = v
if l.DefaultValue != nil {
if rv, ok := typesutil.TryNew(typ); ok {
if err := reflectx.UnmarshalText(rv, l.DefaultValue); err != nil {
return nil, fmt.Errorf("default value `%s` can not unmarshal to %s: %s", l.DefaultValue, typ, err)
}
if err := l.Validate(rv); err != nil {
return nil, fmt.Errorf("default value `%s` is not a valid value of %s: %s", l.DefaultValue, v, err)
}
}
}
}
return l, nil
}
func (loader *ValidatorLoader) Validate(v interface{}) error {
err := loader.validate(v)
if err == nil {
return nil
}
if loader.ErrMsg != nil && len(loader.ErrMsg) != 0 {
return stderrors.New(string(loader.ErrMsg))
}
return err
}
func (loader *ValidatorLoader) validate(v interface{}) error {
rv, ok := v.(reflect.Value)
if !ok {
rv = reflect.ValueOf(v)
}
if reflectx.IsEmptyValue(rv) {
if !loader.Optional {
return errors.MissingRequiredFieldError{}
}
if loader.DefaultValue != nil && rv.CanSet() {
err := reflectx.UnmarshalText(rv, loader.DefaultValue)
if err != nil {
return fmt.Errorf("unmarshal default value failed")
}
}
// empty value should not to validate
return nil
}
if loader.Validator == nil {
return nil
}
if loader.PreprocessStage == PreprocessString {
// make sure value over reflect.Value
if rv.CanInterface() {
v = rv.Interface()
}
if textMarshaller, ok := v.(encoding.TextMarshaler); ok {
data, err := textMarshaller.MarshalText()
if err != nil {
return err
}
return loader.Validator.Validate(string(data))
}
}
if rv.Kind() == reflect.Interface {
rv = rv.Elem()
}
return loader.Validator.Validate(reflectx.Indirect(rv))
}