-
Notifications
You must be signed in to change notification settings - Fork 2
/
string_validator_test.go
173 lines (159 loc) · 4.27 KB
/
string_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package validator
import (
"context"
"fmt"
"reflect"
"regexp"
"testing"
"github.com/go-courier/ptr"
"github.com/go-courier/reflectx/typesutil"
"github.com/stretchr/testify/require"
)
func TestStringValidator_New(t *testing.T) {
caseSet := map[reflect.Type][]struct {
rule string
expect *StringValidator
}{
reflect.TypeOf(""): {
{"@string[1,1000]", &StringValidator{
MinLength: 1,
MaxLength: ptr.Uint64(1000),
}},
{"@string[1,]", &StringValidator{
MinLength: 1,
}},
{"@string<length>[1]", &StringValidator{
MinLength: 1,
MaxLength: ptr.Uint64(1),
}},
{"@char[1,]", &StringValidator{
LenMode: STR_LEN_MODE__RUNE_COUNT,
MinLength: 1,
}},
{"@string<rune_count>[1,]", &StringValidator{
LenMode: STR_LEN_MODE__RUNE_COUNT,
MinLength: 1,
}},
{"@string{KEY1,KEY2}", &StringValidator{
Enums: map[string]string{
"KEY1": "KEY1",
"KEY2": "KEY2",
},
}},
{`@string/^\w+/`, &StringValidator{
Pattern: regexp.MustCompile(`^\w+`),
}},
{`@string/^\w+\/test/`, &StringValidator{
Pattern: regexp.MustCompile(`^\w+/test`),
}},
},
}
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 TestStringValidator_NewFailed(t *testing.T) {
invalidRules := map[reflect.Type][]string{
reflect.TypeOf(1): {
"@string",
},
reflect.TypeOf(""): {
"@string<length, 1>",
"@string<unsupported>",
"@string[1,0]",
"@string[1,-2]",
"@string[a,]",
"@string[-1,1]",
"@string(-1,1)",
},
}
validator := &StringValidator{}
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 TestStringValidator_Validate(t *testing.T) {
type String string
cases := []struct {
values []interface{}
validator *StringValidator
desc string
}{
{[]interface{}{reflect.ValueOf("a"), String("aa"), "aaa", "aaaa", "aaaaa"}, &StringValidator{
MaxLength: ptr.Uint64(5),
}, "less than"},
{[]interface{}{"一", "一一", "一一一"}, &StringValidator{
LenMode: STR_LEN_MODE__RUNE_COUNT,
MaxLength: ptr.Uint64(3),
}, "char count less than"},
{[]interface{}{"A", "B"}, &StringValidator{
Enums: map[string]string{
"A": "A",
"B": "B",
},
}, "in enum"},
{[]interface{}{"word", "word1"}, &StringValidator{
Pattern: regexp.MustCompile(`^\w+`),
}, "regexp matched"},
}
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 TestStringValidator_ValidateFailed(t *testing.T) {
type String string
cases := []struct {
values []interface{}
validator *StringValidator
desc string
}{
{[]interface{}{"C", "D", "E"}, &StringValidator{
Enums: map[string]string{
"A": "A",
"B": "B",
},
}, "enum not match"},
{[]interface{}{"-word", "-word1"}, &StringValidator{
Pattern: regexp.MustCompile(`^\w+`),
}, "regexp not matched"},
{[]interface{}{1.1, reflect.ValueOf(1.1)}, &StringValidator{
MinLength: 5,
}, "unsupported types"},
{[]interface{}{"a", "aa", String("aaa"), []byte("aaaa")}, &StringValidator{
MinLength: 5,
}, "too small"},
{[]interface{}{"aa", "aaa", "aaaa", "aaaaa"}, &StringValidator{
MaxLength: ptr.Uint64(1),
}, "too large"},
{[]interface{}{"字符太多"}, &StringValidator{
LenMode: STR_LEN_MODE__RUNE_COUNT,
MaxLength: ptr.Uint64(3),
}, "too many chars"},
}
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)
})
}
}
}