From 6855bdf56f8f95b3666e79eb72bc5b56eaade60b Mon Sep 17 00:00:00 2001 From: theterminalguy Date: Thu, 24 Nov 2022 13:46:33 -0500 Subject: [PATCH] [Issue-447] Prevent project name from starting with a number (#472) --- internal/init/prompts.go | 4 ++-- internal/init/prompts_test.go | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/init/prompts.go b/internal/init/prompts.go index b2c88078..481aef57 100644 --- a/internal/init/prompts.go +++ b/internal/init/prompts.go @@ -94,13 +94,13 @@ func ValidateSAK(input string) error { // ValidateProjectName validates Project Name field user input. func ValidateProjectName(input string) error { // the first 62 char out of base64 and - - var pName = regexp.MustCompile(`^[A-Za-z0-9-]{1,16}$`) + var pName = regexp.MustCompile(`^[a-zA-Z][A-Za-z0-9-]{1,16}$`) if !pName.MatchString(input) { // error if char len is greater than 16 if len(input) > constants.MaxPnameLength { return errors.New("Invalid, Project Name: (cannot exceed a max length of 16)") } - return errors.New("Invalid, Project Name: (can only contain alphanumeric chars & '-')") + return errors.New("invalid, Project Name: (can only contain alphanumeric chars & '-') and must start with a letter") } return nil } diff --git a/internal/init/prompts_test.go b/internal/init/prompts_test.go index 1a2b5e83..e41045c0 100644 --- a/internal/init/prompts_test.go +++ b/internal/init/prompts_test.go @@ -194,3 +194,20 @@ func TestGetParam(t *testing.T) { assert.Equal(t, "Unsupported custom prompt type random-type.", err.Error()) }) } + +func TestValidateProjectNam(t *testing.T) { + t.Run("Should return error upon invalid project name", func(t *testing.T) { + err := initPrompts.ValidateProjectName("0invalid") + assert.Error(t, err, "Project name should not start with a number") + }) + + t.Run("Should return error upon invalid project name length", func(t *testing.T) { + err := initPrompts.ValidateProjectName("invalid name with more than 30 characters") + assert.Error(t, err, "Project name should not be longer than 30 characters") + }) + + t.Run("Should return nil upon valid project name", func(t *testing.T) { + err := initPrompts.ValidateProjectName("valid-name") + assert.Nil(t, err) + }) +}