-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
r/storage_share_directory: adding validation for the name field
- Loading branch information
1 parent
7214179
commit 472d6eb
Showing
3 changed files
with
81 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package validate | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
func StorageShareDirectoryName(v interface{}, k string) (warnings []string, errors []error) { | ||
value := v.(string) | ||
|
||
// File share names can contain only lowercase letters, numbers, and hyphens, | ||
// and must begin and end with a letter or a number. | ||
if !regexp.MustCompile(`^[a-z0-9][a-z0-9-]+[a-z0-9]$`).MatchString(value) { | ||
errors = append(errors, fmt.Errorf("%s must contain only lowercase alphanumeric characters, numbers and hyphens. It must start and end with a letter and end only with a number or letter", k)) | ||
} | ||
|
||
// The name cannot contain two consecutive hyphens. | ||
if strings.Contains(value, "--") { | ||
errors = append(errors, fmt.Errorf("%s cannot contain two concecutive hyphens", k)) | ||
} | ||
|
||
return warnings, errors | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package validate | ||
|
||
import "testing" | ||
|
||
func TestValidateStorageShareDirectoryName(t *testing.T) { | ||
testCases := []struct { | ||
Input string | ||
Expected bool | ||
}{ | ||
{ | ||
Input: "", | ||
Expected: false, | ||
}, | ||
{ | ||
Input: "abc123", | ||
Expected: true, | ||
}, | ||
{ | ||
Input: "123abc", | ||
Expected: true, | ||
}, | ||
{ | ||
Input: "123-abc", | ||
Expected: true, | ||
}, | ||
{ | ||
Input: "-123-abc", | ||
Expected: false, | ||
}, | ||
{ | ||
Input: "123-abc-", | ||
Expected: false, | ||
}, | ||
{ | ||
Input: "123--abc", | ||
Expected: false, | ||
}, | ||
} | ||
|
||
for _, v := range testCases { | ||
t.Logf("[DEBUG] Test Input %q", v.Input) | ||
|
||
warnings, errors := StorageShareDirectoryName(v.Input, "name") | ||
if len(warnings) != 0 { | ||
t.Fatalf("Expected no warnings but got %d", len(warnings)) | ||
} | ||
|
||
result := len(errors) == 0 | ||
if result != v.Expected { | ||
t.Fatalf("Expected the result to be %t but got %t (and %d errors)", v.Expected, result, len(errors)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters