-
Notifications
You must be signed in to change notification settings - Fork 34
/
validator__map.go
129 lines (104 loc) · 2.7 KB
/
validator__map.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
package validator
import (
"context"
"fmt"
"reflect"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator/errors"
"github.com/machinefi/w3bstream/pkg/depends/kit/validator/rules"
)
var (
TargetMapLength = "map length"
)
type Map struct {
MinProperties uint64
MaxProperties *uint64
KeyValidator Validator
ElemValidator Validator
}
func init() { DefaultFactory.Register(&Map{}) }
func (Map) Names() []string { return []string{"map"} }
func (vm *Map) Validate(v interface{}) error {
switch rv := v.(type) {
case reflect.Value:
return vm.ValidateReflectValue(rv)
default:
return vm.ValidateReflectValue(reflect.ValueOf(v))
}
}
func (vm *Map) ValidateReflectValue(rv reflect.Value) error {
length := uint64(0)
if !rv.IsNil() {
length = uint64(rv.Len())
}
if length < vm.MinProperties {
return &errors.OutOfRangeError{
Target: TargetMapLength,
Current: rv.Interface(),
Minimum: vm.MinProperties,
}
}
if vm.MaxProperties != nil && length > *vm.MaxProperties {
return &errors.OutOfRangeError{
Target: TargetMapLength,
Current: rv.Interface(),
Maximum: vm.MaxProperties,
}
}
if vm.KeyValidator != nil || vm.ElemValidator != nil {
errs := errors.NewErrorSet()
for _, key := range rv.MapKeys() {
vk := key.Interface()
if vm.KeyValidator != nil {
err := vm.KeyValidator.Validate(vk)
if err != nil {
errs.AddErr(err, fmt.Sprintf("%v/key", vk))
}
}
if vm.ElemValidator != nil {
err := vm.ElemValidator.Validate(rv.MapIndex(key).Interface())
if err != nil {
errs.AddErr(err, fmt.Sprintf("%v", vk))
}
}
}
return errs.Err()
}
return nil
}
func (vm *Map) New(ctx context.Context, r *Rule) (Validator, error) {
if r.Type.Kind() != reflect.Map {
return nil, errors.NewUnsupportedTypeError(r.String(), vm.String())
}
ret := &Map{}
if r.ExclusiveMin || r.ExclusiveMax {
return nil, errors.NewSyntaxError(
"range mark of %s should not be `(` or `)`", ret.Names()[0],
)
}
min, max, err := UintRuleRange(r, "size of map", 64)
if err != nil {
return nil, err
}
ret.MinProperties, ret.MaxProperties = min, max
k, v, err := MapRuleParam(ctx, r)
if err != nil {
return nil, err
}
ret.KeyValidator = k
ret.ElemValidator = v
return ret, nil
}
func (vm *Map) String() string {
rule := rules.NewRule(vm.Names()[0])
if vm.KeyValidator != nil || vm.ElemValidator != nil {
rule.Params = make([]rules.Node, 2)
if vm.KeyValidator != nil {
rule.Params[0] = rules.NewLiteral([]byte(vm.KeyValidator.String()))
}
if vm.ElemValidator != nil {
rule.Params[1] = rules.NewLiteral([]byte(vm.ElemValidator.String()))
}
}
rule.Range = RangeFromUint(vm.MinProperties, vm.MaxProperties)
return string(rule.Bytes())
}