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

helper/validation: Wrapper function for legacy SchemaValidateFunc #611

Merged
merged 5 commits into from
Dec 18, 2020
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
29 changes: 29 additions & 0 deletions helper/validation/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"reflect"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand Down Expand Up @@ -57,3 +59,30 @@ func Any(validators ...schema.SchemaValidateFunc) schema.SchemaValidateFunc {
return allWarnings, allErrors
}
}

// ToDiagFunc is a wrapper for legacy schema.SchemaValidateFunc
// converting it to schema.SchemaValidateDiagFunc
func ToDiagFunc(validator schema.SchemaValidateFunc) schema.SchemaValidateDiagFunc {
return func(i interface{}, p cty.Path) diag.Diagnostics {
var diags diag.Diagnostics

attr := p[len(p)-1].(cty.GetAttrStep)
ws, es := validator(i, attr.Name)

for _, w := range ws {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: w,
AttributePath: p,
})
}
for _, e := range es {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: e.Error(),
AttributePath: p,
})
}
return diags
}
}
35 changes: 35 additions & 0 deletions helper/validation/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,38 @@ func TestValidationAny(t *testing.T) {
},
})
}

func TestToDiagFunc(t *testing.T) {
runDiagTestCases(t, []diagTestCase{
{
val: 43,
f: ToDiagFunc(Any(
IntAtLeast(42),
IntAtMost(5),
)),
},
{
val: "foo",
f: ToDiagFunc(All(
StringLenBetween(1, 10),
StringIsNotWhiteSpace,
)),
},
{
val: 7,
f: ToDiagFunc(Any(
IntAtLeast(42),
IntAtMost(5),
)),
expectedErr: regexp.MustCompile("expected [\\w]+ to be at least \\(42\\), got 7"),
},
{
val: 7,
f: ToDiagFunc(Any(
IntAtLeast(42),
IntAtMost(5),
)),
expectedErr: regexp.MustCompile("expected [\\w]+ to be at most \\(5\\), got 7"),
},
})
}
63 changes: 51 additions & 12 deletions helper/validation/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (

testing "github.com/mitchellh/go-testing-interface"

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

Expand All @@ -14,20 +16,15 @@ type testCase struct {
expectedErr *regexp.Regexp
}

type diagTestCase struct {
val interface{}
f schema.SchemaValidateDiagFunc
expectedErr *regexp.Regexp
}

func runTestCases(t testing.T, cases []testCase) {
t.Helper()

matchErr := func(errs []error, r *regexp.Regexp) bool {
// err must match one provided
for _, err := range errs {
if r.MatchString(err.Error()) {
return true
}
}

return false
}

for i, tc := range cases {
_, errs := tc.f(tc.val, "test_property")

Expand All @@ -39,8 +36,50 @@ func runTestCases(t testing.T, cases []testCase) {
t.Fatalf("expected test case %d to produce no errors, got %v", i, errs)
}

if !matchErr(errs, tc.expectedErr) {
if !matchAnyError(errs, tc.expectedErr) {
t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, errs)
}
}
}

func matchAnyError(errs []error, r *regexp.Regexp) bool {
// err must match one provided
for _, err := range errs {
if r.MatchString(err.Error()) {
return true
}
}
return false
}

func runDiagTestCases(t testing.T, cases []diagTestCase) {
t.Helper()

for i, tc := range cases {
p := cty.Path{
cty.GetAttrStep{Name: "test_property"},
}
diags := tc.f(tc.val, p)

if !diags.HasError() && tc.expectedErr == nil {
continue
}

if diags.HasError() && tc.expectedErr == nil {
t.Fatalf("expected test case %d to produce no errors, got %v", i, diags)
}

if !matchAnyDiagSummary(diags, tc.expectedErr) {
t.Fatalf("expected test case %d to produce error matching \"%s\", got %v", i, tc.expectedErr, diags)
}
}
}

func matchAnyDiagSummary(ds diag.Diagnostics, r *regexp.Regexp) bool {
for _, d := range ds {
if r.MatchString(d.Summary) {
return true
}
}
return false
}