From c36f5f989c893c8f640567d175829eaeffe818ab Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Tue, 19 Mar 2024 16:17:07 -0700 Subject: [PATCH 1/2] Update RepoKey validator to match web UI --- validator/validator.go | 9 +++++-- validator/validator_test.go | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/validator/validator.go b/validator/validator.go index 897bdcc..c98543e 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -66,8 +66,13 @@ var CommaSeperatedList = validation.ToDiagFunc( validation.StringMatch(regexp.MustCompile(`.+(?:,.+)*`), "must be comma separated string"), ) -var RepoKey = validation.ToDiagFunc( - validation.StringDoesNotContainAny(" !@#$%^&*()+={}[]:;<>,/?~`|\\"), +var RepoKey = validation.AllDiag( + validation.ToDiagFunc( + validation.StringLenBetween(1, 64), + ), + validation.ToDiagFunc( + validation.StringDoesNotContainAny(" !@#$%^&*()+={}[]:;<>,/?~`|\\"), + ), ) var ProjectKey = validation.ToDiagFunc( diff --git a/validator/validator_test.go b/validator/validator_test.go index cc00915..256502e 100644 --- a/validator/validator_test.go +++ b/validator/validator_test.go @@ -5,6 +5,53 @@ import ( "testing" ) +func TestRepoKey(t *testing.T) { + t.Parallel() + + validRepoKeys := []string{ + "a", // min 1 + "abcd123456789012345678912345678901234567890123456789012345678901", // max 64 + "abc-123", // hyphen is supported + "abc123-", // hyphen can be anywhere + "123abc123", // begin with number + } + + for _, repoKey := range validRepoKeys { + k := repoKey + t.Run(k, func(t *testing.T) { + t.Parallel() + + diag := RepoKey(k, nil) + if diag.HasError() { + t.Errorf("RepoKey validation failed. diag: %v", diag) + } + }) + } +} + +func TestRepoKey_invalidKeys(t *testing.T) { + t.Parallel() + + invalidRepoKeys := []string{ + "", // empty + " ", // has space + "abcd1234567890123456789123456789012345678901234567890123456789012", // 65 chars, too long + "abc,", // invalid characters + } + + for _, repoKey := range invalidRepoKeys { + k := repoKey + t.Run(k, func(t *testing.T) { + t.Parallel() + + diag := RepoKey(k, nil) + if !diag.HasError() { + t.Errorf("RepoKey '%s' should fail", k) + } + }) + } +} + func TestProjectKey(t *testing.T) { t.Parallel() From 2f44f842acbd42f7ff5caa6018fd9cae47e0db83 Mon Sep 17 00:00:00 2001 From: Alex Hung Date: Tue, 19 Mar 2024 16:17:44 -0700 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c10770..4469bba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 1.22.1 (Mar 20, 2024) + +IMPROVEMENTS: + +* Improve `RepoKey` validation to match Artifactory web UI. + +PR: [#55](https://github.com/jfrog/terraform-provider-shared/pull/55) + ## 1.22.0 (Mar 11, 2024) IMPROVEMENTS: