This repository has been archived by the owner on Jun 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
verify.go
123 lines (110 loc) · 4.41 KB
/
verify.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
package controller
import (
"github.com/MegaShow/goagenda/lib/log"
"github.com/MegaShow/goagenda/lib/verify"
"math"
"time"
)
// Username's length should between 1 and 32.
// Username includes only letters, digits and underline, and it must start with letter.
//
// If username is empty, it will pass the check.
func verifyUser(user string) {
if user == "" {
return
}
log.Verbose("check if parameter user matches rules")
verify.AssertLength(1, 32, user, `user name too long
the length of user name can't be larger than 32`)
verify.AssertReg(`^[a-zA-Z][a-zA-Z0-9_]{0,31}$`, user, `user name invalid
username includes only letters, digits and underline, and it must start with letter`)
}
// Username's length should between 1 and 32.
// Username includes only letters, digits and underline, and it must start with letter.
func verifyNonNilUser(user string) {
log.Verbose("check if parameter user matches rules")
verify.AssertNonNil(user, `user name can't be empty`)
verify.AssertLength(1, 32, user, `user name too long
the length of user name can't be larger than 32`)
verify.AssertReg(`^[a-zA-Z][a-zA-Z0-9_]{0,31}$`, user, `user name invalid
username includes only letters, digits and underline, and it must start with letter`)
}
// Password's length should between 6 and 64.
//
// If password is empty, it will pass the check.
func verifyPassword(password string) {
if password == "" {
return
}
log.Verbose("check if parameter password matches rules")
verify.AssertLength(6, 64, password, `password too long or too short
the length of password can't be larger than 64 and shorter than 6`)
}
// Password's length should between 6 and 64.
func verifyNonNilPassword(password string) {
log.Verbose("check if parameter password matches rules")
verify.AssertNonNil(password, `password can't be empty`)
verify.AssertLength(6, 64, password, `password too long or too short
the length of password can't be larger than 64 and shorter than 6`)
}
// Email should match the follow regexp.
//
// If email is empty, it will pass the check.
func verifyEmail(email string) {
if email == "" {
return
}
log.Verbose("check if parameter email matches rules")
verify.AssertReg(`^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$`, email, "email invalid")
}
// Telephone should match the follow regexp, and doesn't support country/area code.
//
// If telephone is empty, it will pass the check.
func verifyTelephone(telephone string) {
if telephone == "" {
return
}
log.Verbose("check if parameter telephone matches rules")
verify.AssertReg(`^((1[3-8][0-9])+\d{8})$`, telephone, "telephone invalid")
}
// Title shouldn't be empty.
func verifyNonNilTitle(title string) {
log.Verbose("check if parameter title matches rules")
verify.AssertNonNil(title, `title can't be empty`)
}
// Start time shouldn't be empty.
// The format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m.
// Time is of 24-hour.
func verifyNonNilStartTime(t time.Time) {
log.Verbose("check if parameter start time matches rules")
verify.AssertTimeNonEqual(t, time.Unix(0, 0), `start time can't be empty`)
verify.AssertTimeNonEqual(t, time.Unix(0, 1), `start time invalid
the format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m, and time is of 24-hour`)
}
// The format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m.
// Time is of 24-hour.
func verifyStartTime(t time.Time) {
log.Verbose("check if parameter start time matches rules")
verify.AssertTimeNonEqual(t, time.Unix(0, 1), `start time invalid
the format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m, and time is of 24-hour`)
}
// End time shouldn't be empty.
// The format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m.
// Time is of 24-hour.
func verifyNonNilEndTime(t time.Time) {
log.Verbose("check if parameter end time matches rules")
verify.AssertTimeNonEqual(t, time.Unix(0, 0), `end time can't be empty`)
verify.AssertTimeNonEqual(t, time.Unix(0, 1), `end time invalid
the format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m, and time is of 24-hour`)
}
// The format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m.
// Time is of 24-hour.
func verifyEndTime(t time.Time) {
log.Verbose("check if parameter end time matches rules")
verify.AssertTimeNonEqual(t, time.Unix(0, 1), `end time invalid
the format of time is YYYY-MM-DD/hh:mm or YYYY-M-D/h:m, and time is of 24-hour`)
}
func verifyNonNilParticipator(arr []string) {
log.Verbose("check if parameter participator matches rules")
verify.AssertArrayLength(1, math.MaxInt32, arr, `participator can't be empty`)
}