feat: 0.0.1
This commit is contained in:
146 files changed
+10444
No files matched your search
@@ -0,0 +1,72 @@
|
||||
package val
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/mail"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var (
|
||||
isValidUsername = regexp.MustCompile(`^[a-zA-Z0-9_]+$`).MatchString
|
||||
isValidFullName = regexp.MustCompile(`^[a-zA-Z\s]+$`).MatchString
|
||||
)
|
||||
|
||||
func ValidateString(val string, minLength int, maxLength int) error {
|
||||
n := len(val)
|
||||
if n < minLength || n > maxLength {
|
||||
return fmt.Errorf("string length must be between %d and %d characters", minLength, maxLength)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateUsername(val string) error {
|
||||
err := ValidateString(val, 3, 100)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid username: %v", err)
|
||||
}
|
||||
|
||||
if !isValidUsername(val) {
|
||||
return fmt.Errorf("username can only contain lowercase alphanumeric characters and underscores")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateFullName(val string) error {
|
||||
err := ValidateString(val, 3, 100)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid full name: %v", err)
|
||||
}
|
||||
|
||||
if !isValidFullName(val) {
|
||||
return fmt.Errorf("full name can only contain alphabetic characters and spaces")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidatePassword(val string) error {
|
||||
return ValidateString(val, 6, 100)
|
||||
}
|
||||
|
||||
func ValidateEmail(val string) error {
|
||||
err := ValidateString(val, 3, 200)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid username: %v", err)
|
||||
}
|
||||
|
||||
if _, err := mail.ParseAddress(val); err != nil {
|
||||
return fmt.Errorf("is not a valid email address")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateEmailID(val int64) error {
|
||||
if val <= 0 {
|
||||
return fmt.Errorf("email ID must be a positive integer")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateVerificationCode(val string) error {
|
||||
return ValidateString(val, 32, 128)
|
||||
}
|
||||
Reference in new issue
Block a user