From ff955e429f0556035295616a9d1735f43708e2e9 Mon Sep 17 00:00:00 2001 From: Gagan Trivedi Date: Tue, 19 Sep 2023 14:14:52 +0530 Subject: [PATCH] fix(fs/value): Add validator for feature state string value (#112) * fix(fs/value): Validate feature state value Prevent feature state values with leading or trailing whitespace, as the API removes them, leading to state mismatches. * tests(fs/value): Add test for regex validator * infra(ci/tests): Update terraform versions --- .github/workflows/test.yml | 6 +++--- flagsmith/resource_feature_state.go | 8 ++++++++ flagsmith/resource_feature_state_test.go | 6 ++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4529f13..446e0fc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,7 @@ permissions: env: # Go language version to use for building. This value should also be updated # in the release workflow if changed. - GO_VERSION: '1.19' + GO_VERSION: '1.20' jobs: # Ensure project builds before running testing matrix @@ -63,8 +63,8 @@ jobs: matrix: # list whatever Terraform versions here you would like to support terraform: - - '1.0.*' - - '1.1.*' + - '1.4.*' + - '1.5.*' steps: - uses: actions/setup-go@v4 with: diff --git a/flagsmith/resource_feature_state.go b/flagsmith/resource_feature_state.go index b5b1531..2fbc8bb 100644 --- a/flagsmith/resource_feature_state.go +++ b/flagsmith/resource_feature_state.go @@ -3,6 +3,7 @@ package flagsmith import ( "context" "fmt" + "regexp" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/tfsdk" @@ -94,6 +95,13 @@ func (t *featureStateResource) Schema(ctx context.Context, req resource.SchemaRe "string_value": schema.StringAttribute{ MarkdownDescription: "String value of the feature if the type is `unicode`.", Optional: true, + Validators: []validator.String{ + // Validate string value satisfies the regular expression for no leading or trailing whitespace + stringvalidator.RegexMatches( + regexp.MustCompile(`^\S[\s\S]*\S$`), + "Leading and trailing whitespace is not allowed", + ), + }, }, "integer_value": schema.Int64Attribute{ MarkdownDescription: "Integer value of the feature if the type is `int`", diff --git a/flagsmith/resource_feature_state_test.go b/flagsmith/resource_feature_state_test.go index e92f0ff..7be9a9c 100644 --- a/flagsmith/resource_feature_state_test.go +++ b/flagsmith/resource_feature_state_test.go @@ -22,6 +22,12 @@ func TestAccEnvironmentFeatureStateResource(t *testing.T) { Config: testAccInvalidFeatureStateValueConfig(), ExpectError: regexp.MustCompile(`Exactly one of these attributes must be configured:\n\[feature_state_value.string_value,feature_state_value.integer_value,feature_state_value.boolean_value\]`), + }, + // Test feature State string value validator + { + Config: testAccEnvironmentFeatureStateResourceConfig(" some_value ", true), + ExpectError: regexp.MustCompile(`Attribute feature_state_value.string_value Leading and trailing whitespace is\n.*not allowed`), + }, // Create and Read testing