Skip to content

Commit 6855bdf

Browse files
[Issue-447] Prevent project name from starting with a number (#472)
1 parent 6d58bdc commit 6855bdf

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

internal/init/prompts.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ func ValidateSAK(input string) error {
9494
// ValidateProjectName validates Project Name field user input.
9595
func ValidateProjectName(input string) error {
9696
// the first 62 char out of base64 and -
97-
var pName = regexp.MustCompile(`^[A-Za-z0-9-]{1,16}$`)
97+
var pName = regexp.MustCompile(`^[a-zA-Z][A-Za-z0-9-]{1,16}$`)
9898
if !pName.MatchString(input) {
9999
// error if char len is greater than 16
100100
if len(input) > constants.MaxPnameLength {
101101
return errors.New("Invalid, Project Name: (cannot exceed a max length of 16)")
102102
}
103-
return errors.New("Invalid, Project Name: (can only contain alphanumeric chars & '-')")
103+
return errors.New("invalid, Project Name: (can only contain alphanumeric chars & '-') and must start with a letter")
104104
}
105105
return nil
106106
}

internal/init/prompts_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,20 @@ func TestGetParam(t *testing.T) {
194194
assert.Equal(t, "Unsupported custom prompt type random-type.", err.Error())
195195
})
196196
}
197+
198+
func TestValidateProjectNam(t *testing.T) {
199+
t.Run("Should return error upon invalid project name", func(t *testing.T) {
200+
err := initPrompts.ValidateProjectName("0invalid")
201+
assert.Error(t, err, "Project name should not start with a number")
202+
})
203+
204+
t.Run("Should return error upon invalid project name length", func(t *testing.T) {
205+
err := initPrompts.ValidateProjectName("invalid name with more than 30 characters")
206+
assert.Error(t, err, "Project name should not be longer than 30 characters")
207+
})
208+
209+
t.Run("Should return nil upon valid project name", func(t *testing.T) {
210+
err := initPrompts.ValidateProjectName("valid-name")
211+
assert.Nil(t, err)
212+
})
213+
}

0 commit comments

Comments
 (0)