-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
192 lines (169 loc) · 4.61 KB
/
validation.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
// Package validation provides methods to validate the various models for this
// project.
package validation
import (
"errors"
"fmt"
"net/mail"
"strings"
"unicode"
"github.com/rs401/letsgorip/pb"
)
var (
// ErrEmptyUid error for empty uid
ErrEmptyUid = errors.New("uid cannot be empty")
// ErrEmptyName error for empty name
ErrEmptyName = errors.New("name cannot be empty")
// ErrEmptyEmail error for empty email
ErrEmptyEmail = errors.New("email cannot be empty")
// ErrEmptyPassword error for empty password
ErrEmptyPassword = errors.New("password cannot be empty")
// ErrEmptyTitle error for empty Title
ErrEmptyTitle = errors.New("title cannot be empty")
// ErrEmptyDescription error for empty Description
ErrEmptyDescription = errors.New("description cannot be empty")
// ErrEmptyMsg error for empty Msg
ErrEmptyMsg = errors.New("message cannot be empty")
// ErrInvalidEmail error for invalid email
ErrInvalidEmail = errors.New("email not valid")
// ErrEmailExists error for email already exists
ErrEmailExists = errors.New("email already exists")
// ErrNameExists error for name already exists
ErrNameExists = errors.New("name already exists")
// ErrNotFound error for not found
ErrNotFound = errors.New("resource not found")
// ErrInvalidPassword error for invalid password
ErrInvalidPassword = errors.New("invalid password, 8-50 characters, one upper, lower, number and special character")
maxPwLen int = 50
minPwLen int = 8
)
// IsValidSignUp takes a *SignUpRequest and verifies if the request is valid
func IsValidSignUp(user *pb.User) error {
if IsEmptyString(user.Uid) {
return ErrEmptyUid
}
if IsEmptyString(user.Name) {
return ErrEmptyName
}
if IsEmptyString(user.Email) {
return ErrEmptyEmail
}
if !IsValidEmail(user.Email) {
return ErrInvalidEmail
}
return nil
}
// IsValidForum verifies the given forum is valid.
func IsValidForum(forum *pb.Forum) error {
if IsEmptyString(forum.Title) {
return ErrEmptyTitle
}
if IsEmptyString(forum.Description) {
return ErrEmptyDescription
}
if forum.UserId == 0 {
// it shouldn't get this far without the UserId
return fmt.Errorf("user id can't be null")
}
return nil
}
// IsValidThread verifies the given thread is valid.
func IsValidThread(thread *pb.Thread) error {
if IsEmptyString(thread.Title) {
return ErrEmptyTitle
}
if IsEmptyString(thread.Msg) {
return ErrEmptyMsg
}
if thread.UserId == 0 {
// it shouldn't get this far without the UserId
return fmt.Errorf("user id can't be null")
}
if thread.ForumId == 0 {
// it shouldn't get this far without the ForumId
return fmt.Errorf("forum id can't be null")
}
return nil
}
// IsValidPost verifies the given post is valid.
func IsValidPost(post *pb.Post) error {
if IsEmptyString(post.Msg) {
return ErrEmptyMsg
}
if post.UserId == 0 {
// it shouldn't get this far without the UserId
return fmt.Errorf("user id can't be null")
}
if post.ThreadId == 0 {
// it shouldn't get this far without the ThreadId
return fmt.Errorf("thread id can't be null")
}
return nil
}
// IsValidPlace verifies the given place is valid.
func IsValidPlace(place *pb.Place) error {
// Name not empty
if IsEmptyString(place.Name) {
return ErrEmptyName
}
// Description not empty
if IsEmptyString(place.Description) {
return ErrEmptyDescription
}
if place.UserId == 0 {
// it shouldn't get this far without the UserId
return fmt.Errorf("user id can't be null")
}
// Lat & Long shouldn't be zero
if place.Latitude == 0 || place.Longitude == 0 {
return fmt.Errorf("coordinates cannot be zero")
}
return nil
}
// IsEmptyString verifies if a string is empty
func IsEmptyString(in string) bool {
return strings.TrimSpace(in) == ""
}
// IsValidEmail verifies if an email is valid
func IsValidEmail(email string) bool {
_, err := mail.ParseAddress(email)
return err == nil
}
// IsValidPassword verifies if a password is valid
func IsValidPassword(s string) bool {
special := false
number := false
upper := false
lower := false
// Check length
if len(s) < minPwLen || len(s) > maxPwLen {
return false
}
// Check other requirements
for _, c := range s {
if special && number && upper && lower {
break
}
switch {
case unicode.IsUpper(c):
upper = true
case unicode.IsLower(c):
lower = true
case unicode.IsNumber(c):
number = true
case unicode.IsPunct(c) || unicode.IsSymbol(c):
special = true
}
}
for _, v := range []bool{special, number, upper, lower} {
if !v {
return false
}
}
// No errors
return true
}
// NormalizeEmail normalizes email string
func NormalizeEmail(email string) string {
return strings.TrimSpace(strings.ToLower(email))
}