Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove double quoting in string validator descriptions #152

Merged
merged 2 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}
Loading