Skip to content

Instantly share code, notes, and snippets.

@marvinhosea
Last active February 20, 2023 08:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marvinhosea/034cf7d09d7b0627d26d20586fa1cd53 to your computer and use it in GitHub Desktop.
Save marvinhosea/034cf7d09d7b0627d26d20586fa1cd53 to your computer and use it in GitHub Desktop.
Main-password-validator.go
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