Skip to content

Commit

Permalink
[Issue-447] Prevent project name from starting with a number (#472)
Browse files Browse the repository at this point in the history
  • Loading branch information
theterminalguy authored Nov 24, 2022
1 parent 6d58bdc commit 6855bdf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/init/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
17 changes: 17 additions & 0 deletions internal/init/prompts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

0 comments on commit 6855bdf

Please sign in to comment.