Skip to content

Commit

Permalink
chore(checkfield): update regexp of resource id
Browse files Browse the repository at this point in the history
  • Loading branch information
donch1989 committed Nov 22, 2023
1 parent 96477a4 commit 25b3d52
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
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")
}

0 comments on commit 25b3d52

Please sign in to comment.