Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(checkfield): update regexp of resource id #11

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions checkfield/checkfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@ func CheckResourceID(id string) error {
return fmt.Errorf("`id` is not allowed to be a UUID")
}

if match, _ := regexp.MatchString("^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$", id); !match {
return fmt.Errorf("`id` needs to be within ASCII-only 63 characters following RFC-1034 with a regexp (^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$)")
if match, _ := regexp.MatchString("^[a-z_][a-z_0-9]{0,31}$", id); !match {
return fmt.Errorf("the ID must consist only of lowercase letters, numbers, or underscores, and its length cannot exceed 32 characters")
}
return nil
}
Expand Down
10 changes: 5 additions & 5 deletions checkfield/checkfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,23 @@ func TestCheckUpdateImmutableFields_UpdateImmutableNestedStruct(t *testing.T) {
}

func TestCheckResourceID_Valid(t *testing.T) {
err := checkfield.CheckResourceID("local-user")
err := checkfield.CheckResourceID("local_user")
require.NoError(t, err)
}

func TestCheckResourceID_InvalidShort(t *testing.T) {
// 0-charactor string
tooShort := ""
err := checkfield.CheckResourceID(tooShort)
require.EqualError(t, err, "`id` needs to be within ASCII-only 63 characters following RFC-1034 with a regexp (^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$)")
require.EqualError(t, err, "the ID must consist only of lowercase letters, numbers, or underscores, and its length cannot exceed 32 characters")
}

func TestCheckResourceID_InvalidLong(t *testing.T) {

// 64-charactor string
tooLong := "abcdefghijklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPQRSTUVWXYZ-0123456789"
err := checkfield.CheckResourceID(tooLong)
require.EqualError(t, err, "`id` needs to be within ASCII-only 63 characters following RFC-1034 with a regexp (^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$)")
require.EqualError(t, err, "the ID must consist only of lowercase letters, numbers, or underscores, and its length cannot exceed 32 characters")
}

func TestCheckResourceID_InvalidUUID(t *testing.T) {
Expand All @@ -375,7 +375,7 @@ func TestCheckResourceID_InvalidUUID(t *testing.T) {
}

func TestCheckResourceID_Invalid(t *testing.T) {
a := "local_user"
a := "local-user"
err := checkfield.CheckResourceID(a)
require.EqualError(t, err, "`id` needs to be within ASCII-only 63 characters following RFC-1034 with a regexp (^[a-z]([a-z0-9-]{0,61}[a-z0-9])?$)")
require.EqualError(t, err, "the ID must consist only of lowercase letters, numbers, or underscores, and its length cannot exceed 32 characters")
}
Loading