Skip to content

Commit

Permalink
add validation for user created names
Browse files Browse the repository at this point in the history
closes #39
  • Loading branch information
djelusic committed Oct 20, 2021
1 parent df5aee7 commit 9d3fc99
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cli/cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ func newNew(a newArgs) (*newCmd, error) {
}

func (c *newCmd) run() error {
if err := workspace.ValidateName(c.name); err != nil {
return log.WithUserMessage(err, fmt.Sprintf("Validation error: %v", err))
}
projectPath, _ := filepath.Abs(c.name)
repo, err := c.repoURL()
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions cli/cmd/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func newStage(a stageArgs) (*stageCmd, error) {
}

func (c *stageCmd) new() error {
if err := workspace.ValidateName(c.stage); err != nil {
return log.WithUserMessage(err, fmt.Sprintf("Validation error: %v", err))
}

if c.account == "" {
accounts := c.store.Workspace().AccountNames()
if len(accounts) > 1 {
Expand Down
46 changes: 45 additions & 1 deletion workspace/validate.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package workspace

import "fmt"
import (
"fmt"
"regexp"
)

var reservedFunctionNames = []string{
"public",
Expand All @@ -22,3 +25,44 @@ type ErrReservedName struct {
func (e *ErrReservedName) Error() string {
return fmt.Sprintf("name \"%s\" is reserved", e.Name)
}

type ErrNameTooLong struct {
Name string
MaxLength int
}

func (e *ErrNameTooLong) Error() string {
return fmt.Sprintf("the name %s is too long, maximum allowed length is %d", e.Name, e.MaxLength)
}

type ErrForbiddenCharacters struct {
Name string
AllowedCharacters string
}

func (e *ErrForbiddenCharacters) Error() string {
return fmt.Sprintf("the name %s contains forbidden characters, it must only contain the following: %s", e.Name, e.AllowedCharacters)
}

const (
maxNameLength = 16
allowedCharactersDescription = "numbers, letters and the special character -"
)

var allowedCharactersRegex = regexp.MustCompile(`^[a-zA-Z0-9\-]+$`)

func ValidateName(name string) error {
if len(name) > maxNameLength {
return &ErrNameTooLong{
Name: name,
MaxLength: maxNameLength,
}
}
if !allowedCharactersRegex.MatchString(name) {
return &ErrForbiddenCharacters{
Name: name,
AllowedCharacters: allowedCharactersDescription,
}
}
return nil
}
54 changes: 54 additions & 0 deletions workspace/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package workspace

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValidateName(t *testing.T) {
type testcase struct {
input string
errorType error
}

cases := []testcase{
{
input: "name",
errorType: nil,
},
{
input: "NAME",
errorType: nil,
},
{
input: "name-123",
errorType: nil,
},
{
input: "some-very-long-name",
errorType: &ErrNameTooLong{},
},
{
input: "name_123",
errorType: &ErrForbiddenCharacters{},
},
{
input: "neko-dugačko-ime",
errorType: &ErrNameTooLong{},
},
{
input: "kraće-ime",
errorType: &ErrForbiddenCharacters{},
},
}

for _, c := range cases {
err := ValidateName(c.input)
if c.errorType == nil {
assert.Nil(t, err)
} else {
assert.IsType(t, c.errorType, err)
}
}
}

0 comments on commit 9d3fc99

Please sign in to comment.