Last active
February 20, 2023 08:59
-
-
Save marvinhosea/034cf7d09d7b0627d26d20586fa1cd53 to your computer and use it in GitHub Desktop.
Main-password-validator.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func passwordValidator(password string) error { | |
var errs []error | |
// validates if password is empty | |
if password == "" { | |
errs = append(errs, ErrPasswordRequired) | |
} | |
// validates if password contains small(lowercase) letters | |
pattern1 := `[a-z]` | |
ok, err := regexp.MatchString(pattern1, password) | |
if err != nil { | |
errs = append(errs, err) | |
} else if !ok { | |
errs = append(errs, ErrSmallLettersRequired) | |
} | |
// validates if password contains capital(uppercase) letters | |
pattern2 := `[A-Z]` | |
ok, err = regexp.MatchString(pattern2, password) | |
if err != nil { | |
errs = append(errs, err) | |
} else if !ok { | |
errs = append(errs, ErrCapitalRequired) | |
} | |
// validate if password contains special characters | |
pattern3 := `"[~!@#$%^&*()-_+=[\]{}|\\;:\"<>,./?]"` | |
ok, err = regexp.MatchString(pattern3, password) | |
if err != nil { | |
errs = append(errs, err) | |
} else if !ok { | |
errs = append(errs, ErrSpecialCharRequired) | |
} | |
// Basic password validation | |
if password != "pas$0Word" && errs == nil { | |
errs = append(errs, ErrPasswordDontMatch) | |
} | |
return errors.Join(errs...) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment