Skip to content

Commit c45f8b0

Browse files
committed
Updates following code review (#12)
1 parent 5896064 commit c45f8b0

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

setvalidator/type_validation_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestValidateSet(t *testing.T) {
7171
gotSetElems, gotOk := validateSet(context.Background(), testCase.request, &tfsdk.ValidateAttributeResponse{})
7272

7373
if diff := cmp.Diff(gotSetElems, testCase.expectedSetElems); diff != "" {
74-
t.Errorf("unexpected float64 difference: %s", diff)
74+
t.Errorf("unexpected set difference: %s", diff)
7575
}
7676

7777
if diff := cmp.Diff(gotOk, testCase.expectedOk); diff != "" {

setvalidator/values_are.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import (
66
"strings"
77

88
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
9-
10-
"github.com/hashicorp/terraform-plugin-framework-validators/validatordiag"
119
)
1210

1311
var _ tfsdk.AttributeValidator = valuesAreValidator{}
@@ -39,14 +37,16 @@ func (v valuesAreValidator) Validate(ctx context.Context, req tfsdk.ValidateAttr
3937
return
4038
}
4139

42-
for k, elem := range elems {
40+
for _, elem := range elems {
4341
value, err := elem.ToTerraformValue(ctx)
4442
if err != nil {
45-
resp.Diagnostics.Append(validatordiag.AttributeValueTerraformValueDiagnostic(
46-
req.AttributePath,
47-
fmt.Sprintf("element at index: %d cannot be converted to Terraform value", k),
48-
err.Error(),
49-
))
43+
resp.Diagnostics.AddError(
44+
"Attribute Conversion Error During Set Element Validation",
45+
"An unexpected error was encountered when handling the a Set element. "+
46+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
47+
"Please report this to the provider developer:\n\n"+
48+
"Attribute Conversion Error During Set Element Validation.",
49+
)
5050
return
5151
}
5252

@@ -58,13 +58,17 @@ func (v valuesAreValidator) Validate(ctx context.Context, req tfsdk.ValidateAttr
5858

5959
for _, validator := range v.valueValidators {
6060
validator.Validate(ctx, request, resp)
61-
if resp.Diagnostics.HasError() {
62-
return
63-
}
6461
}
6562
}
6663
}
6764

65+
// ValuesAre returns an AttributeValidator which ensures that any configured
66+
// attribute value:
67+
//
68+
// - Is a Set.
69+
// - Contains Set elements, each of which validate against each value validator.
70+
//
71+
// Null (unconfigured) and unknown (known after apply) values are skipped.
6872
func ValuesAre(valueValidators ...tfsdk.AttributeValidator) tfsdk.AttributeValidator {
6973
return valuesAreValidator{
7074
valueValidators: valueValidators,

validatordiag/diag.go

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,6 @@ func AttributeValueMatchesDiagnostic(path *tftypes.AttributePath, description st
3535
)
3636
}
3737

38-
// AttributeValueTerraformValueDiagnostic returns an error Diagnostic to be used when an attribute's value cannot be
39-
// converted to terraform value.
40-
func AttributeValueTerraformValueDiagnostic(path *tftypes.AttributePath, description string, value string) diag.Diagnostic {
41-
return diag.NewAttributeErrorDiagnostic(
42-
path,
43-
"Invalid Attribute Value Terraform Value",
44-
capitalize(description)+", err: "+value,
45-
)
46-
}
47-
4838
// capitalize will uppercase the first letter in a UTF-8 string.
4939
func capitalize(str string) string {
5040
if str == "" {

0 commit comments

Comments
 (0)