-
Notifications
You must be signed in to change notification settings - Fork 3
/
signup.go
60 lines (45 loc) · 1.77 KB
/
signup.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
package signup
import (
"errors"
"strings"
"github.com/hooto/httpsrv"
"github.com/hooto/iam/iamapi"
)
func Validate(params *httpsrv.Params) error {
params.SetValue("uname", strings.ToLower(strings.TrimSpace(params.Value("uname"))))
if len(params.Value("uname")) < 4 || len(params.Value("uname")) > 30 {
return errors.New("Username must be between 4 and 30 characters long")
}
if matched := iamapi.UsernameRE.MatchString(params.Value("uname")); !matched {
return errors.New("Username must consist of letters or numbers, and begin with a letter")
}
params.SetValue("email", strings.TrimSpace(strings.ToLower(params.Value("email"))))
if matched := iamapi.EmailRE.MatchString(params.Value("email")); !matched {
return errors.New("Email is not valid")
}
// params.SetValue("name", strings.TrimSpace(params.Value("name")))
// if len(params.Value("name")) == 0 || len(params.Value("name")) > 30 {
// return errors.New("Name must be between 1 and 30 characters long")
// }
if len(params.Value("passwd")) < 8 || len(params.Value("passwd")) > 30 {
return errors.New("Password must be between 8 and 30 characters long")
}
return nil
}
func ValidateEmail(user *iamapi.User) error {
user.Email = strings.ToLower(strings.TrimSpace(user.Email))
if matched := iamapi.EmailRE.MatchString(user.Email); !matched {
return errors.New("Email is not valid")
}
return nil
}
func ValidateUsername(user *iamapi.User) error {
user.Name = strings.ToLower(strings.TrimSpace(user.Name))
if len(user.Name) < 4 || len(user.Name) > 30 {
return errors.New("Username must be between 4 and 30 characters long")
}
if matched := iamapi.UsernameRE.MatchString(user.Name); !matched {
return errors.New("Username must consist of letters or numbers, and begin with a letter")
}
return nil
}