Skip to content

Commit

Permalink
fix(fs/value): Add validator for feature state string value (#112)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
gagantrivedi authored Sep 19, 2023
1 parent eaab495 commit ff955e4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 8 additions & 0 deletions flagsmith/resource_feature_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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`",
Expand Down
6 changes: 6 additions & 0 deletions flagsmith/resource_feature_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ff955e4

Please sign in to comment.