-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.go
74 lines (60 loc) · 1.7 KB
/
validator.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
package gvalidator
import (
"fmt"
"net/mail"
"regexp"
)
var (
isValidUsername = regexp.MustCompile(`^[a-zA-Z0-9_]+$`).MatchString
isValidFullName = regexp.MustCompile(`^[a-zA-Z\s]+$`).MatchString
)
// ValidateString validates the given input between the given min and max length.
func ValidateString(input string, min, max int) error {
if len(input) < min || len(input) > max {
return fmt.Errorf("input must be between %d and %d characters", min, max)
}
return nil
}
// ValidateUsername validates the given username.
func ValidateUsername(username string) error {
if err := ValidateString(username, 3, 16); err != nil {
return err
}
if !isValidUsername(username) {
return fmt.Errorf("username must be between 3 and 16 characters and must contain only lowercase letters, numbers and underscores")
}
return nil
}
// ValidateFullName validates the given name.
func ValidateFullName(value string) error {
if err := ValidateString(value, 3, 100); err != nil {
return err
}
if !isValidFullName(value) {
return fmt.Errorf("must contain only letters or spaces")
}
return nil
}
// ValidatePassword validates the given password.
func ValidatePassword(password string) error {
return ValidateString(password, 6, 32)
}
// ValidateEmail validates the given email.
func ValidateEmail(email string) error {
if err := ValidateString(email, 3, 200); err != nil {
return err
}
if _, err := mail.ParseAddress(email); err != nil {
return fmt.Errorf("not a valid email address")
}
return nil
}
func ValidateEmailID(value int64) error {
if value <= 0 {
return fmt.Errorf("must be a positive integer")
}
return nil
}
func ValidateSecretCode(value string) error {
return ValidateString(value, 32, 128)
}