-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom_test.go
More file actions
110 lines (93 loc) · 2.1 KB
/
custom_test.go
File metadata and controls
110 lines (93 loc) · 2.1 KB
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
package validator_ext_test
import (
"fmt"
ve "github.com/darwinOrg/go-validator-ext"
"testing"
)
type User struct {
Name string `binding:"required,regex=^[a-zA-Z]+$"`
Age int `binding:"max=35,min=22"`
Hobby []string `binding:"maxLength=4,minLength=2"`
Email string `binding:"email"`
Mobile string `binding:"isMobile"`
Country string `binding:"isCountry"`
Birth string `binding:"isDate"`
CreatedAt string `binding:"isDatetime"`
Status int32 `binding:"oneof=0 1"`
Works []*Work `binding:"dive"`
}
type Work struct {
Name string `binding:"maxLength=10,unique=Name"`
}
var user = &User{
Name: "cb",
Age: 30,
Hobby: []string{"1", "2", "3"},
Mobile: "+8615901431753",
Country: "BONAIRE_SINT_EUSTATIUS_AND_SABA",
Email: "123456789@163.com",
Birth: "1990-10-16",
CreatedAt: "2023-03-23 18:00:00",
Status: 0,
Works: []*Work{
{Name: "abc1"},
{Name: "abc2"},
{Name: "swergwsegawegwgwg"},
{Name: "jtrhsgberhehe"},
},
}
func TestNotNull(t *testing.T) {
user.Name = ""
doValidate(user)
}
func TestRegex(t *testing.T) {
user.Name = "cb01"
doValidate(user)
}
func TestMaxValue(t *testing.T) {
user.Age = 36
doValidate(user)
}
func TestMinValue(t *testing.T) {
user.Age = 20
doValidate(user)
}
func TestMaxLength(t *testing.T) {
//user.Hobby = []string{"1", "2", "3", "4", "5"}
doValidate(user)
}
func TestMinLength(t *testing.T) {
user.Hobby = []string{"1"}
doValidate(user)
}
func TestIsMobile(t *testing.T) {
user.Mobile = "+861590"
doValidate(user)
}
func TestIsCountry(t *testing.T) {
user.Country = "XXX"
doValidate(user)
}
func TestIsEmail(t *testing.T) {
user.Email = "123456789"
doValidate(user)
}
func TestIsDate(t *testing.T) {
user.Birth = "1990/10/16"
doValidate(user)
}
func TestIsDateTime(t *testing.T) {
user.CreatedAt = "2023-03-23"
doValidate(user)
}
func TestOneOf(t *testing.T) {
user.Status = 2
doValidate(user)
}
func doValidate(user *User) {
err := ve.NewCustomValidator().Struct(user)
transErrors := ve.TranslateError(err, "zh")
for _, v := range transErrors {
fmt.Println(v)
}
}