-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap_validator.go
177 lines (145 loc) · 4.04 KB
/
map_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
package validator
import (
"context"
"fmt"
"reflect"
"github.com/go-courier/validator/errors"
"github.com/go-courier/validator/rules"
)
var (
TargetMapLength = "map length"
)
/*
Validator for map
Rules:
@map<KEY_RULE, ELEM_RULE>[minSize,maxSize]
@map<KEY_RULE, ELEM_RULE>[length]
@map<@string{A,B,C},@int[0]>[,100]
*/
type MapValidator struct {
MinProperties uint64
MaxProperties *uint64
KeyValidator Validator
ElemValidator Validator
}
func init() {
ValidatorMgrDefault.Register(&MapValidator{})
}
func (MapValidator) Names() []string {
return []string{"map"}
}
func (validator *MapValidator) Validate(v interface{}) error {
switch rv := v.(type) {
case reflect.Value:
return validator.ValidateReflectValue(rv)
default:
return validator.ValidateReflectValue(reflect.ValueOf(v))
}
}
func (validator *MapValidator) ValidateReflectValue(rv reflect.Value) error {
lenOfValue := uint64(0)
if !rv.IsNil() {
lenOfValue = uint64(rv.Len())
}
if lenOfValue < validator.MinProperties {
return &errors.OutOfRangeError{
Target: TargetMapLength,
Current: rv.Interface(),
Minimum: validator.MinProperties,
}
}
if validator.MaxProperties != nil && lenOfValue > *validator.MaxProperties {
return &errors.OutOfRangeError{
Target: TargetMapLength,
Current: rv.Interface(),
Maximum: validator.MaxProperties,
}
}
if validator.KeyValidator != nil || validator.ElemValidator != nil {
errors := errors.NewErrorSet("")
for _, key := range rv.MapKeys() {
vOfKey := key.Interface()
if validator.KeyValidator != nil {
err := validator.KeyValidator.Validate(vOfKey)
if err != nil {
errors.AddErr(err, fmt.Sprintf("%v/key", vOfKey))
}
}
if validator.ElemValidator != nil {
err := validator.ElemValidator.Validate(rv.MapIndex(key).Interface())
if err != nil {
errors.AddErr(err, fmt.Sprintf("%v", vOfKey))
}
}
}
return errors.Err()
}
return nil
}
func (validator *MapValidator) New(ctx context.Context, rule *Rule) (Validator, error) {
if rule.Type.Kind() != reflect.Map {
return nil, errors.NewUnsupportedTypeError(rule.String(), validator.String())
}
mapValidator := &MapValidator{}
if rule.ExclusiveLeft || rule.ExclusiveRight {
return nil, errors.NewSyntaxError("range mark of %s should not be `(` or `)`", mapValidator.Names()[0])
}
if rule.Range != nil {
min, max, err := UintRange("size of map", 64, rule.Range...)
if err != nil {
return nil, err
}
mapValidator.MinProperties = min
mapValidator.MaxProperties = max
}
if rule.Params != nil {
if len(rule.Params) != 2 {
return nil, fmt.Errorf("map should only 2 parameter, but got %d", len(rule.Params))
}
mgr := ValidatorMgrFromContext(ctx)
for i, param := range rule.Params {
switch r := param.(type) {
case *rules.Rule:
switch i {
case 0:
v, err := mgr.Compile(ctx, r.RAW, rule.Type.Key(), nil)
if err != nil {
return nil, fmt.Errorf("map key %s", err)
}
mapValidator.KeyValidator = v
case 1:
v, err := mgr.Compile(ctx, r.RAW, rule.Type.Elem(), nil)
if err != nil {
return nil, fmt.Errorf("map elem %s", err)
}
mapValidator.ElemValidator = v
}
case *rules.RuleLit:
raw := r.Bytes()
if len(raw) > 0 {
return nil, fmt.Errorf("map parameter should be a valid rule")
}
v, err := mgr.Compile(ctx, raw, rule.Type.Elem(), nil)
if err != nil {
return nil, fmt.Errorf("map elem %s", err)
}
mapValidator.ElemValidator = v
}
}
}
return mapValidator, nil
}
func (validator *MapValidator) String() string {
rule := rules.NewRule(validator.Names()[0])
if validator.KeyValidator != nil || validator.ElemValidator != nil {
rule.Params = make([]rules.RuleNode, 2)
if validator.KeyValidator != nil {
rule.Params[0] = rules.NewRuleLit([]byte(validator.KeyValidator.String()))
}
if validator.ElemValidator != nil {
rule.Params[1] = rules.NewRuleLit([]byte(validator.ElemValidator.String()))
}
}
rule.Range = RangeFromUint(validator.MinProperties, validator.MaxProperties)
return string(rule.Bytes())
}