-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator.go
75 lines (58 loc) · 1.53 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
75
package val
import (
"fmt"
"net/mail"
"regexp"
)
var (
isValidUsername = regexp.MustCompile(`^[a-z0-9_]+$`).MatchString
isValidFullname = regexp.MustCompile(`^[a-zA-Z\s]+$`).MatchString
)
func ValidateString(value string, minLength int, maxLength int) error {
n := len(value)
if n < minLength || n > maxLength {
return fmt.Errorf("length must be between %d and %d", minLength, maxLength)
}
return nil
}
func ValidateUsername(username string) error {
err := ValidateString(username, 3, 100)
if err != nil {
return fmt.Errorf("invalid username: %w", err)
}
if !isValidUsername(username) {
return fmt.Errorf("username must contain only lowercase letters, digits and underscores")
}
return nil
}
func ValidateFullname(username string) error {
err := ValidateString(username, 3, 100)
if err != nil {
return fmt.Errorf("invalid username: %w", err)
}
if !isValidFullname(username) {
return fmt.Errorf("fullname must contain only lowercase letters or spaces")
}
return nil
}
func ValidatePassword(password string) error {
return ValidateString(password, 6, 100)
}
func ValidateEmail(email string) error {
if err := ValidateString(email, 6, 100); err != nil {
return fmt.Errorf("invalid email: %w", err)
}
if _, err := mail.ParseAddress(email); err != nil {
return fmt.Errorf("invalid email: %w", err)
}
return nil
}
func ValidateEmailId(value int64) error {
if value <= 0 {
return fmt.Errorf("invalid email id")
}
return nil
}
func ValidateSecretCode(value string) error {
return ValidateString(value, 32, 128)
}