Skip to content

Commit ff955e4

Browse files
authored
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
1 parent eaab495 commit ff955e4

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ permissions:
2020
env:
2121
# Go language version to use for building. This value should also be updated
2222
# in the release workflow if changed.
23-
GO_VERSION: '1.19'
23+
GO_VERSION: '1.20'
2424

2525
jobs:
2626
# Ensure project builds before running testing matrix
@@ -63,8 +63,8 @@ jobs:
6363
matrix:
6464
# list whatever Terraform versions here you would like to support
6565
terraform:
66-
- '1.0.*'
67-
- '1.1.*'
66+
- '1.4.*'
67+
- '1.5.*'
6868
steps:
6969
- uses: actions/setup-go@v4
7070
with:

flagsmith/resource_feature_state.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package flagsmith
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"github.com/hashicorp/terraform-plugin-framework/path"
78
"github.com/hashicorp/terraform-plugin-framework/resource"
89
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
@@ -94,6 +95,13 @@ func (t *featureStateResource) Schema(ctx context.Context, req resource.SchemaRe
9495
"string_value": schema.StringAttribute{
9596
MarkdownDescription: "String value of the feature if the type is `unicode`.",
9697
Optional: true,
98+
Validators: []validator.String{
99+
// Validate string value satisfies the regular expression for no leading or trailing whitespace
100+
stringvalidator.RegexMatches(
101+
regexp.MustCompile(`^\S[\s\S]*\S$`),
102+
"Leading and trailing whitespace is not allowed",
103+
),
104+
},
97105
},
98106
"integer_value": schema.Int64Attribute{
99107
MarkdownDescription: "Integer value of the feature if the type is `int`",

flagsmith/resource_feature_state_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ func TestAccEnvironmentFeatureStateResource(t *testing.T) {
2222
Config: testAccInvalidFeatureStateValueConfig(),
2323
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\]`),
2424

25+
},
26+
// Test feature State string value validator
27+
{
28+
Config: testAccEnvironmentFeatureStateResourceConfig(" some_value ", true),
29+
ExpectError: regexp.MustCompile(`Attribute feature_state_value.string_value Leading and trailing whitespace is\n.*not allowed`),
30+
2531
},
2632

2733
// Create and Read testing

0 commit comments

Comments
 (0)