From 9442ecb55e16dded9d20bb4e86f91264e6b2c25d Mon Sep 17 00:00:00 2001 From: vilmibm Date: Tue, 28 Feb 2023 23:44:34 +0000 Subject: [PATCH] more username validation --- cmd/welcome/main.go | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/cmd/welcome/main.go b/cmd/welcome/main.go index bb4d008..d9f196c 100644 --- a/cmd/welcome/main.go +++ b/cmd/welcome/main.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "os" + "regexp" "strings" "git.tilde.town/tildetown/town/invites" @@ -43,17 +44,34 @@ func promptCode() (code string, err error) { } func promptUsername(townData stats.TildeData) (un string, err error) { + usernameRE := regexp.MustCompile(`^[a-z][-a-z0-9_]*$`) unPrompt := &survey.Input{ Message: "desired username?", } err = survey.AskOne(unPrompt, &un, survey.WithValidator(survey.Required), survey.WithIcons(surveyIconSet), + survey.WithValidator(func(val interface{}) error { + un := val.(string) + if len(un) > 32 { + return fmt.Errorf("username '%s' is too long", un) + } + + return nil + }), + survey.WithValidator(func(val interface{}) error { + un := val.(string) + if !usernameRE.MatchString(un) { + return errors.New("usernames must start with a letter and only contain letters, nubers, - or _") + } + + return nil + }), survey.WithValidator(func(val interface{}) error { un := val.(string) for _, v := range townData.Users { if v.Username == un { - return errors.New("that username is already in use, sorry") + return fmt.Errorf("username '%s' is already in use", un) } } return nil