Skip to content

Commit 7c732e8

Browse files
authored
Move shared type validation functions to separate source file and add unit testing (#26)
Reference: #23 (comment)
1 parent a5644e3 commit 7c732e8

9 files changed

+297
-60
lines changed

float64validator/at_most.go

-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
88
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9-
"github.com/hashicorp/terraform-plugin-framework/types"
109
)
1110

1211
var _ tfsdk.AttributeValidator = atMostValidator{}
@@ -57,22 +56,3 @@ func AtMost(max float64) tfsdk.AttributeValidator {
5756
max: max,
5857
}
5958
}
60-
61-
// validateFloat ensures that the request contains a Float64 value.
62-
func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (float64, bool) {
63-
var n types.Float64
64-
65-
diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)
66-
67-
if diags.HasError() {
68-
response.Diagnostics = append(response.Diagnostics, diags...)
69-
70-
return 0, false
71-
}
72-
73-
if n.Unknown || n.Null {
74-
return 0, false
75-
}
76-
77-
return n.Value, true
78-
}

float64validator/type_validation.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package float64validator
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
)
9+
10+
// validateFloat ensures that the request contains a Float64 value.
11+
func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (float64, bool) {
12+
var n types.Float64
13+
14+
diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)
15+
16+
if diags.HasError() {
17+
response.Diagnostics = append(response.Diagnostics, diags...)
18+
19+
return 0, false
20+
}
21+
22+
if n.Unknown || n.Null {
23+
return 0, false
24+
}
25+
26+
return n.Value, true
27+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package float64validator
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/hashicorp/terraform-plugin-go/tftypes"
11+
)
12+
13+
func TestValidateFloat(t *testing.T) {
14+
t.Parallel()
15+
16+
testCases := map[string]struct {
17+
request tfsdk.ValidateAttributeRequest
18+
expectedFloat64 float64
19+
expectedOk bool
20+
}{
21+
"invalid-type": {
22+
request: tfsdk.ValidateAttributeRequest{
23+
AttributeConfig: types.Bool{Value: true},
24+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
25+
},
26+
expectedFloat64: 0.0,
27+
expectedOk: false,
28+
},
29+
"float64-null": {
30+
request: tfsdk.ValidateAttributeRequest{
31+
AttributeConfig: types.Float64{Null: true},
32+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
33+
},
34+
expectedFloat64: 0.0,
35+
expectedOk: false,
36+
},
37+
"float64-value": {
38+
request: tfsdk.ValidateAttributeRequest{
39+
AttributeConfig: types.Float64{Value: 1.2},
40+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
41+
},
42+
expectedFloat64: 1.2,
43+
expectedOk: true,
44+
},
45+
"float64-unknown": {
46+
request: tfsdk.ValidateAttributeRequest{
47+
AttributeConfig: types.Float64{Unknown: true},
48+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
49+
},
50+
expectedFloat64: 0.0,
51+
expectedOk: false,
52+
},
53+
}
54+
55+
for name, testCase := range testCases {
56+
name, testCase := name, testCase
57+
58+
t.Run(name, func(t *testing.T) {
59+
t.Parallel()
60+
61+
gotFloat64, gotOk := validateFloat(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})
62+
63+
if diff := cmp.Diff(gotFloat64, testCase.expectedFloat64); diff != "" {
64+
t.Errorf("unexpected float64 difference: %s", diff)
65+
}
66+
67+
if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" {
68+
t.Errorf("unexpected ok difference: %s", diff)
69+
}
70+
})
71+
}
72+
}

int64validator/at_most.go

-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
88
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9-
"github.com/hashicorp/terraform-plugin-framework/types"
109
)
1110

1211
var _ tfsdk.AttributeValidator = atMostValidator{}
@@ -57,22 +56,3 @@ func AtMost(max int64) tfsdk.AttributeValidator {
5756
max: max,
5857
}
5958
}
60-
61-
// validateInt ensures that the request contains an Int64 value.
62-
func validateInt(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (int64, bool) {
63-
var n types.Int64
64-
65-
diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)
66-
67-
if diags.HasError() {
68-
response.Diagnostics = append(response.Diagnostics, diags...)
69-
70-
return 0, false
71-
}
72-
73-
if n.Unknown || n.Null {
74-
return 0, false
75-
}
76-
77-
return n.Value, true
78-
}

int64validator/type_validation.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package int64validator
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
)
9+
10+
// validateInt ensures that the request contains an Int64 value.
11+
func validateInt(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (int64, bool) {
12+
var n types.Int64
13+
14+
diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &n)
15+
16+
if diags.HasError() {
17+
response.Diagnostics = append(response.Diagnostics, diags...)
18+
19+
return 0, false
20+
}
21+
22+
if n.Unknown || n.Null {
23+
return 0, false
24+
}
25+
26+
return n.Value, true
27+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package int64validator
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/hashicorp/terraform-plugin-go/tftypes"
11+
)
12+
13+
func TestValidateInt(t *testing.T) {
14+
t.Parallel()
15+
16+
testCases := map[string]struct {
17+
request tfsdk.ValidateAttributeRequest
18+
expectedInt64 int64
19+
expectedOk bool
20+
}{
21+
"invalid-type": {
22+
request: tfsdk.ValidateAttributeRequest{
23+
AttributeConfig: types.Bool{Value: true},
24+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
25+
},
26+
expectedInt64: 0.0,
27+
expectedOk: false,
28+
},
29+
"int64-null": {
30+
request: tfsdk.ValidateAttributeRequest{
31+
AttributeConfig: types.Int64{Null: true},
32+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
33+
},
34+
expectedInt64: 0.0,
35+
expectedOk: false,
36+
},
37+
"int64-value": {
38+
request: tfsdk.ValidateAttributeRequest{
39+
AttributeConfig: types.Int64{Value: 123},
40+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
41+
},
42+
expectedInt64: 123,
43+
expectedOk: true,
44+
},
45+
"int64-unknown": {
46+
request: tfsdk.ValidateAttributeRequest{
47+
AttributeConfig: types.Int64{Unknown: true},
48+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
49+
},
50+
expectedInt64: 0.0,
51+
expectedOk: false,
52+
},
53+
}
54+
55+
for name, testCase := range testCases {
56+
name, testCase := name, testCase
57+
58+
t.Run(name, func(t *testing.T) {
59+
t.Parallel()
60+
61+
gotInt64, gotOk := validateInt(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})
62+
63+
if diff := cmp.Diff(gotInt64, testCase.expectedInt64); diff != "" {
64+
t.Errorf("unexpected float64 difference: %s", diff)
65+
}
66+
67+
if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" {
68+
t.Errorf("unexpected ok difference: %s", diff)
69+
}
70+
})
71+
}
72+
}

stringvalidator/length_at_most.go

-20
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
88
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9-
"github.com/hashicorp/terraform-plugin-framework/types"
109
)
1110

1211
var _ tfsdk.AttributeValidator = lengthAtMostValidator{}
@@ -61,22 +60,3 @@ func LengthAtMost(maxLength int) tfsdk.AttributeValidator {
6160
maxLength: maxLength,
6261
}
6362
}
64-
65-
// validateString ensures that the request contains a String value.
66-
func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (string, bool) {
67-
var s types.String
68-
69-
diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &s)
70-
71-
if diags.HasError() {
72-
response.Diagnostics = append(response.Diagnostics, diags...)
73-
74-
return "", false
75-
}
76-
77-
if s.Unknown || s.Null {
78-
return "", false
79-
}
80-
81-
return s.Value, true
82-
}

stringvalidator/type_validation.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package stringvalidator
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
7+
"github.com/hashicorp/terraform-plugin-framework/types"
8+
)
9+
10+
// validateString ensures that the request contains a String value.
11+
func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest, response *tfsdk.ValidateAttributeResponse) (string, bool) {
12+
var s types.String
13+
14+
diags := tfsdk.ValueAs(ctx, request.AttributeConfig, &s)
15+
16+
if diags.HasError() {
17+
response.Diagnostics = append(response.Diagnostics, diags...)
18+
19+
return "", false
20+
}
21+
22+
if s.Unknown || s.Null {
23+
return "", false
24+
}
25+
26+
return s.Value, true
27+
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package stringvalidator
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/google/go-cmp/cmp"
8+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9+
"github.com/hashicorp/terraform-plugin-framework/types"
10+
"github.com/hashicorp/terraform-plugin-go/tftypes"
11+
)
12+
13+
func TestValidateString(t *testing.T) {
14+
t.Parallel()
15+
16+
testCases := map[string]struct {
17+
request tfsdk.ValidateAttributeRequest
18+
expectedString string
19+
expectedOk bool
20+
}{
21+
"invalid-type": {
22+
request: tfsdk.ValidateAttributeRequest{
23+
AttributeConfig: types.Bool{Value: true},
24+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
25+
},
26+
expectedString: "",
27+
expectedOk: false,
28+
},
29+
"string-null": {
30+
request: tfsdk.ValidateAttributeRequest{
31+
AttributeConfig: types.Int64{Null: true},
32+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
33+
},
34+
expectedString: "",
35+
expectedOk: false,
36+
},
37+
"string-value": {
38+
request: tfsdk.ValidateAttributeRequest{
39+
AttributeConfig: types.String{Value: "test-value"},
40+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
41+
},
42+
expectedString: "test-value",
43+
expectedOk: true,
44+
},
45+
"string-unknown": {
46+
request: tfsdk.ValidateAttributeRequest{
47+
AttributeConfig: types.Int64{Unknown: true},
48+
AttributePath: tftypes.NewAttributePath().WithAttributeName("test"),
49+
},
50+
expectedString: "",
51+
expectedOk: false,
52+
},
53+
}
54+
55+
for name, testCase := range testCases {
56+
name, testCase := name, testCase
57+
58+
t.Run(name, func(t *testing.T) {
59+
t.Parallel()
60+
61+
gotInt64, gotOk := validateString(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})
62+
63+
if diff := cmp.Diff(gotInt64, testCase.expectedString); diff != "" {
64+
t.Errorf("unexpected float64 difference: %s", diff)
65+
}
66+
67+
if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" {
68+
t.Errorf("unexpected ok difference: %s", diff)
69+
}
70+
})
71+
}
72+
}

0 commit comments

Comments
 (0)