From 6928dd639308c71991d1aae6b25bf59089e59458 Mon Sep 17 00:00:00 2001 From: "Chang, Hui-Tang" Date: Wed, 22 Nov 2023 17:17:15 +0800 Subject: [PATCH] chore(checkfield): update regexp of resource id (#11) Because - we are going to use snake_case style for resource id This commit - update regexp of resource id --- checkfield/checkfield.go | 4 ++-- checkfield/checkfield_test.go | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/checkfield/checkfield.go b/checkfield/checkfield.go index 63ca0bb..87f981b 100644 --- a/checkfield/checkfield.go +++ b/checkfield/checkfield.go @@ -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 } diff --git a/checkfield/checkfield_test.go b/checkfield/checkfield_test.go index e792163..c221d41 100644 --- a/checkfield/checkfield_test.go +++ b/checkfield/checkfield_test.go @@ -349,7 +349,7 @@ 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) } @@ -357,7 +357,7 @@ 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) { @@ -365,7 +365,7 @@ 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) { @@ -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") }