Skip to content

Commit 2299f55

Browse files
authored
all: Included missing attribute path details in error diagnostics (#61)
Reference: #57 Reference: hashicorp/terraform#31575 Terraform output currently does not include the attribute path details if the attribute is not present in the configuration. This can make troubleshooting difficult for practitioners and provider developers. Even if Terraform implements path details for unconfigured attributes, those details may not necessarily be clear for troubleshooting as they would likely be implemented in a very short, concise manner to reduce diagnostic message noise. Those details would also only be present on recent and future Terraform versions, while these validators support all versions of Terraform supported by the terraform-plugin-framework Go module, currently Terraform 0.12 and later.
1 parent 7425c06 commit 2299f55

File tree

7 files changed

+22
-18
lines changed

7 files changed

+22
-18
lines changed

.changelog/61.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
all: Included missing attribute path details in error diagnostics since they are currently not output by Terraform
3+
```

float64validator/type_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest,
1414
if t != types.Float64Type {
1515
response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
1616
request.AttributePath,
17-
"Expected value of type float64",
17+
"expected value of type float64",
1818
t.String(),
1919
))
2020
return 0.0, false

helpers/validatordiag/diag.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package validatordiag
22

33
import (
4+
"fmt"
45
"unicode"
56
"unicode/utf8"
67

@@ -13,7 +14,7 @@ func InvalidAttributeValueDiagnostic(path path.Path, description string, value s
1314
return diag.NewAttributeErrorDiagnostic(
1415
path,
1516
"Invalid Attribute Value",
16-
capitalize(description)+", got: "+value,
17+
fmt.Sprintf("Attribute %s %s, got: %s", path, description, value),
1718
)
1819
}
1920

@@ -22,7 +23,7 @@ func InvalidAttributeValueLengthDiagnostic(path path.Path, description string, v
2223
return diag.NewAttributeErrorDiagnostic(
2324
path,
2425
"Invalid Attribute Value Length",
25-
capitalize(description)+", got: "+value,
26+
fmt.Sprintf("Attribute %s %s, got: %s", path, description, value),
2627
)
2728
}
2829

@@ -31,7 +32,7 @@ func InvalidAttributeValueMatchDiagnostic(path path.Path, description string, va
3132
return diag.NewAttributeErrorDiagnostic(
3233
path,
3334
"Invalid Attribute Value Match",
34-
capitalize(description)+", got: "+value,
35+
fmt.Sprintf("Attribute %s %s, got: %s", path, description, value),
3536
)
3637
}
3738

@@ -49,7 +50,7 @@ func InvalidAttributeTypeDiagnostic(path path.Path, description string, value st
4950
return diag.NewAttributeErrorDiagnostic(
5051
path,
5152
"Invalid Attribute Type",
52-
capitalize(description)+", got: "+value,
53+
fmt.Sprintf("Attribute %s %s, got: %s", path, description, value),
5354
)
5455
}
5556

metavalidator/all_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAllValidator(t *testing.T) {
3636
diag.NewAttributeErrorDiagnostic(
3737
path.Root("test"),
3838
"Invalid Attribute Type",
39-
"Expected value of type string, got: types.Int64Type",
39+
"Attribute test expected value of type string, got: types.Int64Type",
4040
),
4141
},
4242
},
@@ -51,7 +51,7 @@ func TestAllValidator(t *testing.T) {
5151
diag.NewAttributeErrorDiagnostic(
5252
path.Root("test"),
5353
"Invalid Attribute Value Length",
54-
"String length must be at least 5, got: 3",
54+
"Attribute test string length must be at least 5, got: 3",
5555
),
5656
},
5757
},

metavalidator/any_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestAnyValidator(t *testing.T) {
3636
diag.NewAttributeErrorDiagnostic(
3737
path.Root("test"),
3838
"Invalid Attribute Type",
39-
"Expected value of type string, got: types.Int64Type",
39+
"Attribute test expected value of type string, got: types.Int64Type",
4040
),
4141
},
4242
},
@@ -51,12 +51,12 @@ func TestAnyValidator(t *testing.T) {
5151
diag.NewAttributeErrorDiagnostic(
5252
path.Root("test"),
5353
"Invalid Attribute Value Length",
54-
"String length must be at least 4, got: 3",
54+
"Attribute test string length must be at least 4, got: 3",
5555
),
5656
diag.NewAttributeErrorDiagnostic(
5757
path.Root("test"),
5858
"Invalid Attribute Value Length",
59-
"String length must be at least 5, got: 3",
59+
"Attribute test string length must be at least 5, got: 3",
6060
),
6161
},
6262
},
@@ -80,12 +80,12 @@ func TestAnyValidator(t *testing.T) {
8080
diag.NewAttributeErrorDiagnostic(
8181
path.Root("test"),
8282
"Invalid Attribute Value Length",
83-
"String length must be at least 6, got: 3",
83+
"Attribute test string length must be at least 6, got: 3",
8484
),
8585
diag.NewAttributeErrorDiagnostic(
8686
path.Root("test"),
8787
"Invalid Attribute Value Length",
88-
"String length must be at least 5, got: 3",
88+
"Attribute test string length must be at least 5, got: 3",
8989
),
9090
},
9191
},

metavalidator/any_with_all_warnings_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func TestAnyWithAllWarningsValidator(t *testing.T) {
5555
diag.NewAttributeErrorDiagnostic(
5656
path.Root("test"),
5757
"Invalid Attribute Type",
58-
"Expected value of type string, got: types.Int64Type",
58+
"Attribute test expected value of type string, got: types.Int64Type",
5959
),
6060
},
6161
},
@@ -70,12 +70,12 @@ func TestAnyWithAllWarningsValidator(t *testing.T) {
7070
diag.NewAttributeErrorDiagnostic(
7171
path.Root("test"),
7272
"Invalid Attribute Value Length",
73-
"String length must be at least 4, got: 3",
73+
"Attribute test string length must be at least 4, got: 3",
7474
),
7575
diag.NewAttributeErrorDiagnostic(
7676
path.Root("test"),
7777
"Invalid Attribute Value Length",
78-
"String length must be at least 5, got: 3",
78+
"Attribute test string length must be at least 5, got: 3",
7979
),
8080
},
8181
},
@@ -99,12 +99,12 @@ func TestAnyWithAllWarningsValidator(t *testing.T) {
9999
diag.NewAttributeErrorDiagnostic(
100100
path.Root("test"),
101101
"Invalid Attribute Value Length",
102-
"String length must be at least 6, got: 3",
102+
"Attribute test string length must be at least 6, got: 3",
103103
),
104104
diag.NewAttributeErrorDiagnostic(
105105
path.Root("test"),
106106
"Invalid Attribute Value Length",
107-
"String length must be at least 5, got: 3",
107+
"Attribute test string length must be at least 5, got: 3",
108108
),
109109
},
110110
},

stringvalidator/type_validation.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest,
1414
if t != types.StringType {
1515
response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic(
1616
request.AttributePath,
17-
"Expected value of type string",
17+
"expected value of type string",
1818
t.String(),
1919
))
2020
return "", false

0 commit comments

Comments
 (0)