-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap_validator_test.go
152 lines (140 loc) · 4.54 KB
/
map_validator_test.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
package validator
import (
"context"
"fmt"
"reflect"
"testing"
"github.com/go-courier/ptr"
"github.com/go-courier/reflectx/typesutil"
"github.com/stretchr/testify/require"
)
func TestMapValidator_New(t *testing.T) {
caseSet := map[reflect.Type][]struct {
rule string
expect *MapValidator
}{
reflect.TypeOf(map[string]string{}): {
{"@map[1,1000]", &MapValidator{
MinProperties: 1,
MaxProperties: ptr.Uint64(1000),
}},
},
reflect.TypeOf(map[string]map[string]string{}): {
{"@map<,@map[1,2]>[1,]", &MapValidator{
MinProperties: 1,
ElemValidator: ValidatorMgrDefault.MustCompile(context.Background(), []byte("@map[1,2]"), typesutil.FromRType(reflect.TypeOf(map[string]string{})), nil),
}},
{"@map<@string[0,],@map[1,2]>[1,]", &MapValidator{
MinProperties: 1,
KeyValidator: ValidatorMgrDefault.MustCompile(context.Background(), []byte("@string[0,]"), typesutil.FromRType(reflect.TypeOf("")), nil),
ElemValidator: ValidatorMgrDefault.MustCompile(context.Background(), []byte("@map[1,2]"), typesutil.FromRType(reflect.TypeOf(map[string]string{})), nil),
}},
},
}
for typ, cases := range caseSet {
for _, c := range cases {
t.Run(fmt.Sprintf("%s %s|%s", typ, c.rule, c.expect.String()), func(t *testing.T) {
v, err := c.expect.New(ContextWithValidatorMgr(context.Background(), ValidatorMgrDefault), MustParseRuleStringWithType(c.rule, typesutil.FromRType(typ)))
require.NoError(t, err)
require.Equal(t, c.expect, v)
})
}
}
}
func TestMapValidator_NewFailed(t *testing.T) {
invalidRules := map[reflect.Type][]string{
reflect.TypeOf([]string{}): {
"@map",
},
reflect.TypeOf(map[string]string{}): {
"@map<1,>",
"@map<,2>",
"@map<1,2,3>",
"@map[1,0]",
"@map[1,-2]",
"@map[a,]",
"@map[-1,1]",
"@map(-1,1)",
"@map<@unknown,>",
"@map<,@unknown>",
"@map<@string[0,],@unknown>",
},
}
validator := &MapValidator{}
for typ := range invalidRules {
for _, r := range invalidRules[typ] {
rule := MustParseRuleStringWithType(r, typesutil.FromRType(typ))
t.Run(fmt.Sprintf("validate %s new failed: %s", typ, rule.Bytes()), func(t *testing.T) {
_, err := validator.New(ContextWithValidatorMgr(context.Background(), ValidatorMgrDefault), rule)
require.Error(t, err)
t.Log(err)
})
}
}
}
func TestMapValidator_Validate(t *testing.T) {
cases := []struct {
values []interface{}
validator *MapValidator
desc string
}{
{[]interface{}{
map[string]string{"1": "", "2": ""},
map[string]string{"1": "", "2": "", "3": ""},
map[string]string{"1": "", "2": "", "3": "", "4": ""},
}, &MapValidator{
MinProperties: 2,
MaxProperties: ptr.Uint64(4),
}, "in range"},
{[]interface{}{
reflect.ValueOf(map[string]string{"1": "", "2": ""}),
map[string]string{"1": "", "2": "", "3": ""},
}, &MapValidator{
MinProperties: 2,
MaxProperties: ptr.Uint64(4),
KeyValidator: ValidatorMgrDefault.MustCompile(context.Background(), []byte("@string[1,]"), typesutil.FromRType(reflect.TypeOf("1")), nil),
ElemValidator: ValidatorMgrDefault.MustCompile(context.Background(), []byte("@string[1,]?"), typesutil.FromRType(reflect.TypeOf("1")), nil),
}, "key value validate"},
}
for _, c := range cases {
for _, v := range c.values {
t.Run(fmt.Sprintf("%s: %s validate %v", c.desc, c.validator, v), func(t *testing.T) {
require.NoError(t, c.validator.Validate(v))
})
}
}
}
func TestMapValidator_ValidateFailed(t *testing.T) {
cases := []struct {
values []interface{}
validator *MapValidator
desc string
}{
{[]interface{}{
map[string]string{"1": ""},
map[string]string{"1": "", "2": "", "3": "", "4": "", "5": ""},
map[string]string{"1": "", "2": "", "3": "", "4": "", "5": "", "6": ""},
}, &MapValidator{
MinProperties: 2,
MaxProperties: ptr.Uint64(4),
}, "out of range"},
{[]interface{}{
map[string]string{"1": "", "2": ""},
map[string]string{"1": "", "2": "", "3": ""},
}, &MapValidator{
MinProperties: 2,
MaxProperties: ptr.Uint64(4),
KeyValidator: ValidatorMgrDefault.MustCompile(context.Background(), []byte("@string[2,]"), typesutil.FromRType(reflect.TypeOf("")), nil),
ElemValidator: ValidatorMgrDefault.MustCompile(context.Background(), []byte("@string[2,]"), typesutil.FromRType(reflect.TypeOf("")), nil),
}, "key elem validate failed"},
}
for _, c := range cases {
for _, v := range c.values {
t.Run(fmt.Sprintf("%s: %s validate %v", c.desc, c.validator, v), func(t *testing.T) {
err := c.validator.Validate(v)
require.Error(t, err)
t.Log(err)
})
}
}
}