Skip to content
Snippets Groups Projects
Commit 31bc1ace authored by David McKay's avatar David McKay Committed by Tamer Tas
Browse files

Added a new validate class, AlphanumericExt, that allows a few extra...

Added a new validate class, AlphanumericExt, that allows a few extra characters ontop of regular Alphanumeric validation. Closes #17 (#25)
parent 10a73994
No related branches found
No related tags found
No related merge requests found
...@@ -18,7 +18,7 @@ var Delete = &cli.Command{ ...@@ -18,7 +18,7 @@ var Delete = &cli.Command{
Use: "delete <template-tag>", Use: "delete <template-tag>",
Short: "Delete a project template from the template registry", Short: "Delete a project template from the template registry",
Run: func(c *cli.Command, args []string) { Run: func(c *cli.Command, args []string) {
MustValidateVarArgs(args, validate.Argument{"template-path", validate.Alphanumeric}) MustValidateVarArgs(args, validate.Argument{"template-path", validate.AlphanumericExt})
MustValidateTemplateDir() MustValidateTemplateDir()
......
...@@ -103,7 +103,7 @@ var Download = &cli.Command{ ...@@ -103,7 +103,7 @@ var Download = &cli.Command{
MustValidateArgs(args, []validate.Argument{ MustValidateArgs(args, []validate.Argument{
{"template-repo", validate.UnixPath}, {"template-repo", validate.UnixPath},
{"template-tag", validate.Alphanumeric}, {"template-tag", validate.AlphanumericExt},
}) })
MustValidateTemplateDir() MustValidateTemplateDir()
......
...@@ -21,7 +21,7 @@ var Save = &cli.Command{ ...@@ -21,7 +21,7 @@ var Save = &cli.Command{
Run: func(c *cli.Command, args []string) { Run: func(c *cli.Command, args []string) {
MustValidateArgs(args, []validate.Argument{ MustValidateArgs(args, []validate.Argument{
{"template-path", validate.UnixPath}, {"template-path", validate.UnixPath},
{"template-tag", validate.Alphanumeric}, {"template-tag", validate.AlphanumericExt},
}) })
MustValidateTemplateDir() MustValidateTemplateDir()
......
...@@ -12,6 +12,7 @@ const ( ...@@ -12,6 +12,7 @@ const (
alphanumeric = "^[a-zA-Z0-9]+$" alphanumeric = "^[a-zA-Z0-9]+$"
integer = "^[-+]?(0|[1-9][0-9]*)$" integer = "^[-+]?(0|[1-9][0-9]*)$"
numeric = "^[-+]?[0-9]+$" numeric = "^[-+]?[0-9]+$"
alphanumericext = "^[a-zA-Z0-9-_]+$"
) )
var ( var (
...@@ -21,6 +22,10 @@ var ( ...@@ -21,6 +22,10 @@ var (
// Alphanumeric regular expression for alphanumeric strings. // Alphanumeric regular expression for alphanumeric strings.
Alphanumeric = regexp.MustCompile(alphanumeric) Alphanumeric = regexp.MustCompile(alphanumeric)
// AlphanumericExt regular expression for alphanumeric strings plus
// certain allowed characters.
AlphanumericExt = regexp.MustCompile(alphanumericext)
// Email regular expression for e-mail strings. // Email regular expression for e-mail strings.
Email = regexp.MustCompile(email) Email = regexp.MustCompile(email)
......
...@@ -48,6 +48,28 @@ func TestAlphanumericPattern(t *testing.T) { ...@@ -48,6 +48,28 @@ func TestAlphanumericPattern(t *testing.T) {
} }
} }
func TestAlphanumericExtPattern(t *testing.T) {
tests := []struct {
String string
Valid bool
}{
{" ", false},
{"/", false},
{"root", true},
{"tmp-dir", true},
{"tmp-dir_now", true},
{"TMPDIR", true},
{"L33T", true},
{"L@@T", false},
}
for _, test := range tests {
if ok := pattern.AlphanumericExt.MatchString(test.String); ok != test.Valid {
t.Errorf("pattern.AlphanumericExt.MatchString(%q) expected to be %v", test.String, test.Valid)
}
}
}
func TestIntegerPattern(t *testing.T) { func TestIntegerPattern(t *testing.T) {
tests := []struct { tests := []struct {
String string String string
......
...@@ -37,3 +37,9 @@ func UnixPath(path string) bool { ...@@ -37,3 +37,9 @@ func UnixPath(path string) bool {
func Alphanumeric(s string) bool { func Alphanumeric(s string) bool {
return pattern.Alphanumeric.MatchString(s) return pattern.Alphanumeric.MatchString(s)
} }
// AlphanumericExt validates whether a string is an alphanumeric string, but a
// small set of extra characters allowed
func AlphanumericExt(s string) bool {
return pattern.AlphanumericExt.MatchString(s)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment