-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
78 additions
and
29 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
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
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
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
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
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
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
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
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,15 @@ | ||
package indexerbase | ||
|
||
import "regexp" | ||
|
||
// NameFormat is the regular expression that a name must match. | ||
// A name must start with a letter or underscore and can only contain letters, numbers, and underscores. | ||
// A name must be at least one character long and can be at most 64 characters long. | ||
const NameFormat = `^[a-zA-Z_][a-zA-Z0-9_]{0,63}$` | ||
|
||
var nameRegex = regexp.MustCompile(NameFormat) | ||
|
||
// ValidateName checks if the given name is a valid name conforming to NameFormat. | ||
func ValidateName(name string) bool { | ||
return nameRegex.MatchString(name) | ||
} |
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,31 @@ | ||
package indexerbase | ||
|
||
import "testing" | ||
|
||
func TestValidateName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
valid bool | ||
}{ | ||
{"", false}, | ||
{"a", true}, | ||
{"A", true}, | ||
{"_", true}, | ||
{"abc123_def789", true}, | ||
{"0", false}, | ||
{"a0", true}, | ||
{"a_", true}, | ||
{"$a", false}, | ||
{"a b", false}, | ||
{"pretty_unnecessarily_long_but_valid_name", true}, | ||
{"totally_unnecessarily_long_and_invalid_name_sdgkhwersdglkhweriqwery3258", false}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
if ValidateName(test.name) != test.valid { | ||
t.Errorf("expected %v for name %q", test.valid, test.name) | ||
} | ||
}) | ||
} | ||
} |
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
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