-
Notifications
You must be signed in to change notification settings - Fork 2
/
validator_loader_test.go
159 lines (143 loc) · 3.25 KB
/
validator_loader_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
package validator
import (
"context"
"fmt"
"reflect"
"testing"
"time"
"github.com/go-courier/ptr"
"github.com/go-courier/reflectx/typesutil"
"github.com/stretchr/testify/require"
)
func TestNewValidatorLoader(t *testing.T) {
type SomeStruct struct {
PtrString *string
String string
}
var val *string
someStruct := &SomeStruct{}
cases := []struct {
valuesPass []interface{}
valuesFailed []interface{}
rule string
typ reflect.Type
validator *ValidatorLoader
}{
{
[]interface{}{
reflect.ValueOf(someStruct).Elem().FieldByName("String"),
"1",
},
[]interface{}{"222"},
"@string[1,2] = '1'",
reflect.TypeOf(""),
&ValidatorLoader{
Optional: true,
DefaultValue: []byte("1"),
PreprocessStage: PreprocessSkip,
},
},
{
[]interface{}{
Duration(1 * time.Second),
Duration(1 * time.Second),
},
[]interface{}{},
"@string",
reflect.TypeOf(Duration(1 * time.Second)),
&ValidatorLoader{
PreprocessStage: PreprocessString,
},
},
{
[]interface{}{
val,
reflect.ValueOf(someStruct).Elem().FieldByName("Value"),
reflect.ValueOf(val),
ptr.String("1"),
},
[]interface{}{
ptr.String("222"),
},
"@string[1,2] = 2",
reflect.TypeOf(ptr.String("")),
&ValidatorLoader{
Optional: true,
DefaultValue: []byte("2"),
PreprocessStage: PreprocessPtr,
},
},
{
[]interface{}{
ptr.String("1"),
ptr.String("22"),
},
[]interface{}{
ptr.String(""),
(*string)(nil),
},
"@string[1,2]",
reflect.TypeOf(ptr.String("")),
&ValidatorLoader{
PreprocessStage: PreprocessPtr,
},
},
}
for _, c := range cases {
t.Run(fmt.Sprintf("%s %s", c.typ, c.rule), func(t *testing.T) {
validator, err := ValidatorMgrDefault.Compile(context.Background(), []byte(c.rule), typesutil.FromRType(c.typ), nil)
require.NoError(t, err)
if err != nil {
return
}
loader := validator.(*ValidatorLoader)
require.Equal(t, c.validator.Optional, loader.Optional)
require.Equal(t, c.validator.PreprocessStage, loader.PreprocessStage)
require.Equal(t, c.validator.DefaultValue, loader.DefaultValue)
for _, v := range c.valuesPass {
err := loader.Validate(v)
require.NoError(t, err)
}
for _, v := range c.valuesFailed {
err := loader.Validate(v)
require.Error(t, err)
}
})
}
}
func TestNewValidatorLoaderFailed(t *testing.T) {
invalidRules := map[reflect.Type][]string{
reflect.TypeOf(1): {
"@string",
"@int[1,2] = s",
},
reflect.TypeOf(""): {
"@string<length, 1>",
"@string[1,2] = 123",
},
reflect.TypeOf(Duration(1)): {
"@string[,10] = 2ss",
},
}
for typ := range invalidRules {
for _, r := range invalidRules[typ] {
t.Run(fmt.Sprintf("%s validate %s", typ, r), func(t *testing.T) {
_, err := ValidatorMgrDefault.Compile(context.Background(), []byte(r), typesutil.FromRType(typ), nil)
require.Error(t, err)
t.Log(err)
})
}
}
}
type Duration time.Duration
func (d Duration) MarshalText() ([]byte, error) {
return []byte(time.Duration(d).String()), nil
}
func (d *Duration) UnmarshalText(data []byte) error {
dur, err := time.ParseDuration(string(data))
if err != nil {
return err
}
*d = Duration(dur)
return nil
}