-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
156 lines (145 loc) · 3.21 KB
/
example_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
package vd_test
import (
"context"
"fmt"
vd "github.com/goclub/validator"
"log"
"testing"
)
func TestExampleQuickStart(t *testing.T) {
ExampleChecker_Check()
}
func ExampleChecker_Check() {
ctx := context.Background()
_ = ctx
err := func() (err error) {
checker := vd.NewCN()
createUser := RequestCreateUser{
Email: "[email protected]",
Name: "张三",
Nickname: "三儿",
Age: 20,
Skills: []string{"clang", "go"},
Address: RequestCreateUserAddress{
Province: "上海",
Detail: "", //
},
AddressList: []RequestCreateUserAddress{
{
Province: "上海",
Detail: "人民广场一号",
},
{
Province: "上海",
Detail: "", // 人民广场一号
},
},
}
report, err := checker.Check(createUser)
if err != nil {
return
}
if report.Fail {
log.Print("fail")
log.Print("path:", report.Path)
log.Print("message:", report.Message)
} else {
log.Print("验证通过")
}
return
}()
if err != nil {
log.Printf("%+v", err)
}
}
type RequestCreateUser struct {
Email string
Name string
Nickname string
Age int
Skills []string
Address RequestCreateUserAddress `json:"address"`
AddressList []RequestCreateUserAddress
}
func (v RequestCreateUser) VD(r *vd.Rule) (err error) {
r.String(v.Email, vd.StringSpec{
Name: "邮箱地址",
Path: "email",
Ext: []vd.StringSpec{vd.ExtString{}.Email()},
})
r.String(v.Name, vd.StringSpec{
Name: "姓名",
Path: "name",
MinRuneLen: 2,
MaxRuneLen: 20,
})
r.String(v.Nickname, vd.StringSpec{
Name: "昵称",
Path: "nickname",
AllowEmpty: true, // 昵称非必填
BanPattern: []string{`\d`},
PatternMessage: "昵称不允许包含数字",
MinRuneLen: 2,
MaxRuneLen: 10,
})
r.Int(v.Age, vd.IntSpec{
Name: "年龄",
Path: "age",
Min: vd.Int(18),
MinMessage: "只允许成年人注册",
})
r.Int(len(v.Skills), vd.IntSpec{
Name: "技能",
Path: "skills",
Max: vd.Int(10),
MaxMessage: "最多填写{{Max}}项",
})
// vdg.Slice(r, v.Skills, vdg.SliceSpec{
// Name: "技能",
// Path: "skills",
// MaxLen: vd.Int(10),
// MaxLenMessage: "最多填写{{MaxLen}}项",
// Unique: true,
// })
for index, skill := range v.Skills {
r.String(skill, vd.StringSpec{
Name: "技能项",
Path: vd.PathIndex("skill", index),
})
}
// Address由 RequestCreateUserAddress{}.VD() 实现
return nil
}
type RequestCreateUserAddress struct {
Province string
Detail string
}
func (v RequestCreateUserAddress) VD(r *vd.Rule) (err error) {
r.String(v.Province, vd.StringSpec{
Path: "province",
Name: "省",
})
r.String(v.Detail, vd.StringSpec{
Name: "详细地址",
Path: "detail",
Pattern: []string{`号`},
PatternMessage: "地址必须包含门牌号,例如:某路110号",
})
return
}
type LogKind uint8
func (v LogKind) Validator(err ...error) error {
switch v {
case 1:
case 2:
default:
return fmt.Errorf("kind can not be %v", v)
}
return nil
}
type Log struct {
Kind LogKind
}
func (v Log) VD(r *vd.Rule) {
r.Validator(v.Kind, "类型格式错误", "")
}