Skip to content

Commit

Permalink
Merge pull request #1473 from oasisprotocol/kostko/feature/identifier…
Browse files Browse the repository at this point in the history
…-update-invalid

client-sdk/go: Update validity rules for config identifiers
  • Loading branch information
kostko authored Sep 3, 2023
2 parents a01f864 + 47803dc commit 43bb365
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
6 changes: 4 additions & 2 deletions client-sdk/go/config/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"regexp"
)

var validID = regexp.MustCompile(`^[a-z0-9_]+$`)
var validID = regexp.MustCompile(`^[a-z0-9][a-z0-9_]*$`)

// ValidateIdentifier makes sure the given string is a valid identifier.
func ValidateIdentifier(id string) error {
Expand All @@ -14,8 +14,10 @@ func ValidateIdentifier(id string) error {
return fmt.Errorf("identifier cannot be empty")
case len(id) > 64:
return fmt.Errorf("identifier must be less than 64 characters long")
case id == "default":
return fmt.Errorf("identifier cannot be the string 'default'")
case !validID.MatchString(id):
return fmt.Errorf("identifier must only contain lower-case letters, numbers and _")
return fmt.Errorf("identifier must start with a lower-case letter or number and only contain lower-case letters, numbers and _")
default:
return nil
}
Expand Down
5 changes: 5 additions & 0 deletions client-sdk/go/config/identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func TestValidateIdentifier(t *testing.T) {
{"222", true},
{"abc", true},
{"abc_2", true},
{"_v", false},
{"_foo", false},
{"default", false},
{"a", true},
{"2", true},
} {
if tc.valid {
require.NoError(ValidateIdentifier(tc.id), tc.id)
Expand Down

0 comments on commit 43bb365

Please sign in to comment.