Last active
February 20, 2023 14:55
-
-
Save marvinhosea/578b332ef8dd21ee89ba8e1838198a03 to your computer and use it in GitHub Desktop.
Main
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
package main | |
import ( | |
"encoding/json" | |
"errors" | |
"log" | |
"regexp" | |
) | |
var ( | |
ErrSmallLettersRequired = errors.New("password must contain small letters") | |
ErrCapitalRequired = errors.New("password must contain capital letters") | |
ErrSpecialCharRequired = errors.New("password must contain a special character") | |
ErrPasswordDontMatch = errors.New("invalid password") | |
) | |
type EmptyPasswordErr struct { | |
Msg string | |
} | |
func (p *EmptyPasswordErr) Error() string { | |
return p.Msg | |
} | |
func main() { | |
password := "password" | |
response := make(map[string]any) | |
var passwordErrs []string | |
err := passwordValidator(password) | |
if err != nil { | |
var emptyPassword *EmptyPasswordErr | |
if errors.As(err, &emptyPassword) { | |
passwordErrs = append(passwordErrs, emptyPassword.Msg) | |
} else { | |
//passwordErrs = append(passwordErrs, strings.Split(err.Error(), "\n")...) | |
if errors.Is(err, ErrCapitalRequired) { | |
passwordErrs = append(passwordErrs, ErrCapitalRequired.Error()) | |
} | |
if errors.Is(err, ErrSmallLettersRequired) { | |
passwordErrs = append(passwordErrs, ErrSmallLettersRequired.Error()) | |
} | |
if errors.Is(err, ErrPasswordDontMatch) { | |
passwordErrs = append(passwordErrs, ErrPasswordDontMatch.Error()) | |
} | |
if errors.Is(err, ErrSpecialCharRequired) { | |
passwordErrs = append(passwordErrs, ErrSpecialCharRequired.Error()) | |
} | |
} | |
response["password"] = passwordErrs | |
} | |
jsonResponse, err := json.Marshal(&response) | |
if err != nil { | |
log.Fatal(err) | |
} | |
log.Println(string(jsonResponse)) | |
} | |
func passwordValidator(password string) error { | |
var errs []error | |
// validates if password is empty | |
if password == "" { | |
emptyPasswordErr := &EmptyPasswordErr{ | |
Msg: "Password is required and must have uppercase, lowercase, and special character.", | |
} | |
errs = append(errs, emptyPasswordErr) | |
} | |
// 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