-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
41 lines (35 loc) · 997 Bytes
/
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
package core
import (
"net/url"
"regexp"
"strings"
)
var emailRegexp = regexp.MustCompile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$")
func validateEmailAddress(email string) error {
if !emailRegexp.MatchString(email) {
return NewUserError("invalid email address")
}
return nil
}
func validatePassword(password string) error {
if len(password) <= 3 {
return NewUserError("password must be at least three characters long")
}
return nil
}
func validateFeedName(name string) error {
if strings.TrimSpace(name) == "" {
return NewUserError("feed name cannot be blank")
}
return nil
}
func validateURL(inputURL string) error {
u, err := url.ParseRequestURI(inputURL)
if err != nil {
return NewUserError("invalid URL")
}
if s := strings.ToLower(u.Scheme); !(s == "http" || s == "https") {
return NewUserError("invalid URL request scheme - must be HTTP or HTTPS")
}
return nil
}