Skip to content

Commit

Permalink
Remove double quoting in string validator descriptions (#152)
Browse files Browse the repository at this point in the history
* Remove double quoting in string validator descriptions (#149)

* Adding changelog entry (#149)
  • Loading branch information
bendbennett authored Aug 3, 2023
1 parent 5fe0aad commit c3ae185
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230801-104745.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'stringvalidator: Removed double quoting in `Description` returned from `NoneOf`,
`NoneOfCaseInsensitive`, `OneOf` and `OneOfCaseInsensitive` validators'
time: 2023-08-01T10:47:45.629204+01:00
custom:
Issue: "152"
2 changes: 1 addition & 1 deletion stringvalidator/none_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (v noneOfValidator) Description(ctx context.Context) string {
}

func (v noneOfValidator) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("value must be none of: %q", v.values)
return fmt.Sprintf("value must be none of: %s", v.values)
}

func (v noneOfValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
Expand Down
2 changes: 1 addition & 1 deletion stringvalidator/none_of_case_insensitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (v noneOfCaseInsensitiveValidator) Description(ctx context.Context) string
}

func (v noneOfCaseInsensitiveValidator) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("value must be none of: %q", v.values)
return fmt.Sprintf("value must be none of: %s", v.values)
}

func (v noneOfCaseInsensitiveValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
Expand Down
35 changes: 34 additions & 1 deletion stringvalidator/none_of_case_insensitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
)

func TestNoneOfCaseInsensitiveValidator(t *testing.T) {
Expand Down Expand Up @@ -93,3 +95,34 @@ func TestNoneOfCaseInsensitiveValidator(t *testing.T) {
})
}
}

func TestNoneOfCaseInsensitiveValidator_Description(t *testing.T) {
t.Parallel()

type testCase struct {
in []string
expected string
}

testCases := map[string]testCase{
"quoted-once": {
in: []string{"foo", "bar", "baz"},
expected: `value must be none of: ["foo" "bar" "baz"]`,
},
}

for name, test := range testCases {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()

v := stringvalidator.NoneOfCaseInsensitive(test.in...)

got := v.MarkdownDescription(context.Background())

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
32 changes: 32 additions & 0 deletions stringvalidator/none_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

Expand Down Expand Up @@ -94,3 +95,34 @@ func TestNoneOfValidator(t *testing.T) {
})
}
}

func TestNoneOfValidator_Description(t *testing.T) {
t.Parallel()

type testCase struct {
in []string
expected string
}

testCases := map[string]testCase{
"quoted-once": {
in: []string{"foo", "bar", "baz"},
expected: `value must be none of: ["foo" "bar" "baz"]`,
},
}

for name, test := range testCases {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()

v := stringvalidator.NoneOf(test.in...)

got := v.MarkdownDescription(context.Background())

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
2 changes: 1 addition & 1 deletion stringvalidator/one_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (v oneOfValidator) Description(ctx context.Context) string {
}

func (v oneOfValidator) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("value must be one of: %q", v.values)
return fmt.Sprintf("value must be one of: %s", v.values)
}

func (v oneOfValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
Expand Down
2 changes: 1 addition & 1 deletion stringvalidator/one_of_case_insensitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (v oneOfCaseInsensitiveValidator) Description(ctx context.Context) string {
}

func (v oneOfCaseInsensitiveValidator) MarkdownDescription(_ context.Context) string {
return fmt.Sprintf("value must be one of: %q", v.values)
return fmt.Sprintf("value must be one of: %s", v.values)
}

func (v oneOfCaseInsensitiveValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
Expand Down
35 changes: 34 additions & 1 deletion stringvalidator/one_of_case_insensitive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (
"context"
"testing"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
)

func TestOneOfCaseInsensitiveValidator(t *testing.T) {
Expand Down Expand Up @@ -93,3 +95,34 @@ func TestOneOfCaseInsensitiveValidator(t *testing.T) {
})
}
}

func TestOneOfCaseInsensitiveValidator_Description(t *testing.T) {
t.Parallel()

type testCase struct {
in []string
expected string
}

testCases := map[string]testCase{
"quoted-once": {
in: []string{"foo", "bar", "baz"},
expected: `value must be one of: ["foo" "bar" "baz"]`,
},
}

for name, test := range testCases {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()

v := stringvalidator.OneOfCaseInsensitive(test.in...)

got := v.MarkdownDescription(context.Background())

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}
32 changes: 32 additions & 0 deletions stringvalidator/one_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"

Expand Down Expand Up @@ -94,3 +95,34 @@ func TestOneOfValidator(t *testing.T) {
})
}
}

func TestOneOfValidator_Description(t *testing.T) {
t.Parallel()

type testCase struct {
in []string
expected string
}

testCases := map[string]testCase{
"quoted-once": {
in: []string{"foo", "bar", "baz"},
expected: `value must be one of: ["foo" "bar" "baz"]`,
},
}

for name, test := range testCases {
name, test := name, test
t.Run(name, func(t *testing.T) {
t.Parallel()

v := stringvalidator.OneOf(test.in...)

got := v.MarkdownDescription(context.Background())

if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
})
}
}

0 comments on commit c3ae185

Please sign in to comment.