diff --git a/pkg/utils/helper.go b/pkg/utils/helper.go index b7312ee4..f498d19a 100644 --- a/pkg/utils/helper.go +++ b/pkg/utils/helper.go @@ -44,27 +44,45 @@ func FormatSize(size int64) string { return fmt.Sprintf("%.2fMiB", mbSize) } +func VaildUserName(username string) bool { + pattern := `^[a-zA-Z0-9]{1,255}$` + re := regexp.MustCompile(pattern) + return re.MatchString(username) +} + +func VaildEmail(email string) bool { + pattern := `^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` + re := regexp.MustCompile(pattern) + return re.MatchString(email) +} + +func VaildFL(name string) bool { + pattern := `^[A-Za-z]{1,20}\s[A-Za-z]{1,20}$` + re := regexp.MustCompile(pattern) + return re.MatchString(name) +} + // check if the password format is vaild -func ValidatePassword(password string) (bool, error) { +func ValidatePassword(password string) error { if len(password) < 8 || len(password) > 256 { - return false, errors.New("worong! the password length must be at least 8 characters and at most 256 characters") + return errors.New("worong! the password length must be at least 8 characters and at most 256 characters") } // checking the password has a minimum of one lower case letter if done, _ := regexp.MatchString("([a-z])+", password); !done { - return false, errors.New("worong! the password doesn't have a lowercase letter") + return errors.New("worong! the password doesn't have a lowercase letter") } // checking the password has a minimmum of one upper case letter if done, _ := regexp.MatchString("([A-Z])+", password); !done { - return false, errors.New("worong! the password doesn't have an upppercase letter") + return errors.New("worong! the password doesn't have an upppercase letter") } // checking if the password has a minimum of one digit if done, _ := regexp.Match("([0-9])+", []byte(password)); !done { - return false, errors.New("worong! the password doesn't have a digit number") + return errors.New("worong! the password doesn't have a digit number") } - return true, errors.New("") + return nil } // check if the tag name is valid @@ -78,7 +96,7 @@ func ValidateTagName(tagName string) bool { // check if the project name is valid func ValidateProjectName(projectName string) bool { - pattern := `^[a-z0-9][a-z0-9._-]{0,254}$$` + pattern := `^[a-z0-9][a-z0-9._-]{0,254}$` re := regexp.MustCompile(pattern) diff --git a/pkg/views/login/create.go b/pkg/views/login/create.go index 7653e8b5..c4e09d62 100644 --- a/pkg/views/login/create.go +++ b/pkg/views/login/create.go @@ -51,8 +51,7 @@ func CreateView(loginView *LoginView) { if strings.TrimSpace(str) == "" { return errors.New("password cannot be empty") } - isVaild, err := utils.ValidatePassword(str) - if !isVaild { + if err := utils.ValidatePassword(str); err != nil { return err } return nil diff --git a/pkg/views/user/create/view.go b/pkg/views/user/create/view.go index fef9ac11..8b1d78df 100644 --- a/pkg/views/user/create/view.go +++ b/pkg/views/user/create/view.go @@ -2,8 +2,10 @@ package create import ( "errors" + "strings" "github.com/charmbracelet/huh" + "github.com/goharbor/harbor-cli/pkg/utils" log "github.com/sirupsen/logrus" ) @@ -23,17 +25,23 @@ func CreateUserView(createView *CreateView) { Title("User Name"). Value(&createView.Username). Validate(func(str string) error { - if str == "" { + if strings.TrimSpace(str) == "" { return errors.New("user name cannot be empty") } + if isVaild := utils.VaildUserName(str); !isVaild { + return errors.New("username cannot contain special characters") + } return nil }), huh.NewInput(). Title("Email"). Value(&createView.Email). Validate(func(str string) error { - if str == "" { - return errors.New("email cannot be empty") + if strings.TrimSpace(str) == "" { + return errors.New("email cannot be empty or only spaces") + } + if isVaild := utils.VaildEmail(str); !isVaild { + return errors.New("please enter correct email format") } return nil }), @@ -42,18 +50,24 @@ func CreateUserView(createView *CreateView) { Title("First and Last Name"). Value(&createView.Realname). Validate(func(str string) error { - if str == "" { + if strings.TrimSpace(str) == "" { return errors.New("real name cannot be empty") } + if isValid := utils.VaildFL(str); !isValid { + return errors.New("please enter correct first and last name format, like John Cooper") + } return nil }), huh.NewInput(). Title("Password"). - EchoMode(huh.EchoModePassword). + EchoMode(huh.EchoModePassword). Value(&createView.Password). Validate(func(str string) error { - if str == "" { - return errors.New("password cannot be empty") + if strings.TrimSpace(str) == "" { + return errors.New("password cannot be empty or only space") + } + if err := utils.ValidatePassword(str); err != nil { + return err } return nil }),