-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
valis_test.go
223 lines (190 loc) · 5.07 KB
/
valis_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package valis_test
import (
"encoding/json"
"errors"
"fmt"
"github.com/soranoba/valis/to"
"github.com/soranoba/valis"
"github.com/soranoba/valis/is"
"github.com/soranoba/valis/tagrule"
"github.com/soranoba/valis/translations"
"github.com/soranoba/valis/when"
"golang.org/x/text/language"
"golang.org/x/text/message"
"golang.org/x/text/message/catalog"
)
func Example() {
type User struct {
Name string
Age int
}
u := &User{}
if err := valis.Validate(
&u,
valis.Field(&u.Name, is.NonZero),
valis.Field(&u.Age, is.Min(20)),
); err != nil {
fmt.Println(err)
}
u.Name = "Alice"
u.Age = 20
if err := valis.Validate(
&u,
valis.Field(&u.Name, is.NonZero),
valis.Field(&u.Age, is.Min(20)),
); err != nil {
fmt.Println(err)
}
// Output:
// (non_zero) .Name can't be blank (or zero)
// (gte) .Age must be greater than or equal to 20
}
func Example_customizeError() {
type User struct {
Name string `json:"name"`
Age int `json:"age"`
}
v := valis.NewValidator()
v.SetErrorCollectorFactoryFunc(func() valis.ErrorCollector {
// You can create ErrorCollectors yourself that will generate your own error.
// If you want to change only the attribute name, please change NameResolver.
return valis.NewStandardErrorCollector(valis.JSONLocationNameResolver)
})
u := User{}
if err := v.Validate(
&u,
valis.Field(&u.Name, is.NonZero),
valis.Field(&u.Age, is.Min(20)),
); err != nil {
fmt.Println(err)
}
// Output:
// (non_zero) .name can't be blank (or zero)
// (gte) .age must be greater than or equal to 20
}
func Example_translate() {
type User struct {
Name string
Age int
}
// It is recommended to have the catalog in a global variable etc. instead of creating it every time.
// We welcome your contributions if it does not exist the language you use.
c := translations.NewCatalog(catalog.Fallback(language.English))
for _, f := range translations.AllPredefinedCatalogRegistrationFunc {
c.Set(f)
}
u := User{}
if err := valis.Validate(
&u,
valis.Field(&u.Name, is.NonZero),
valis.Field(&u.Age, is.Min(20)),
); err != nil {
for _, lang := range []language.Tag{language.English, language.Japanese} {
p := message.NewPrinter(lang, message.Catalog(c))
// When you change the ErrorCollector and create errors other than ValidationError, you need an alternative.
m := err.(*valis.ValidationError).Translate(p)
b, _ := json.MarshalIndent(m, "", " ")
fmt.Printf("%s\n", b)
}
}
// Output:
// {
// ".Age": [
// "must be greater than or equal to 20"
// ],
// ".Name": [
// "can't be blank (or zero)"
// ]
// }
// {
// ".Age": [
// "は20より大きい値にする必要があります"
// ],
// ".Name": [
// "を空白にすることはできません"
// ]
// }
}
func Example_structTag() {
type User struct {
Name *string `required:"true"`
Age int `validate:"min=20"`
}
v := valis.NewValidator()
u := User{}
if err := v.Validate(&u, when.IsStruct(valis.EachFields(tagrule.Required, tagrule.Validate))); err != nil {
fmt.Println(err)
}
// Output:
// (required) .Name is required
// (gte) .Age must be greater than or equal to 20
}
type ValidatableUser struct {
Name string
}
func (u *ValidatableUser) Validate() error {
if u.Name == "" {
return errors.New("name is empty")
}
return nil
}
func Example_validatable() {
v := valis.NewValidator()
// *ValidatableUser is implemented `Validate() error`
// Validate returns nil, when Name is not empty. Otherwise, it returns an error.
user := ValidatableUser{}
if err := v.Validate(&user, valis.ValidatableRule); err != nil {
fmt.Println(err)
}
// Output:
// (custom) name is empty
}
func Example_nestedStruct() {
type User struct {
Name *string `required:"true"`
Age int `validate:"min=20"`
Company struct {
Location *string `required:"true"`
}
}
v := valis.NewValidator()
// Use the CommonRule if you want to automatically search and validate all hierarchies.
v.SetCommonRules(when.IsStruct(valis.EachFields(tagrule.Required, tagrule.Validate)))
user := User{}
if err := v.Validate(&user); err != nil {
fmt.Println(err)
}
// Output:
// (required) .Name is required
// (gte) .Age must be greater than or equal to 20
// (required) .Company.Location is required
}
func Example_flow() {
arr := []interface{}{0, 1, 2, 3, "a", "b", "c", "A", "B", "C"}
v := valis.NewValidator()
if err := v.Validate(
&arr,
valis.Each(
when.IsNumeric(is.In(1, 2, 3)).
Else(is.In("a", "b", "c")),
),
); err != nil {
fmt.Println(err)
}
// Output:
// (inclusion) [0] is not included in [1 2 3]
// (inclusion) [7] is not included in [a b c]
// (inclusion) [8] is not included in [a b c]
// (inclusion) [9] is not included in [a b c]
}
func Example_convert() {
arr := []interface{}{0, 1, 2, 3, "1", "2", "3"}
v := valis.NewValidator()
if err := v.Validate(&arr, valis.Each(to.Int(is.Min(2)))); err != nil {
fmt.Println(err)
}
// Output:
// (gte) [0] must be greater than or equal to 2
// (gte) [1] must be greater than or equal to 2
// (gte) [4] must be greater than or equal to 2
}