From 82d8be887b47765601d7a023ed3b288434b7df32 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Sun, 28 Aug 2022 15:12:42 -0400 Subject: [PATCH 1/2] all: Included missing attribute path details in error diagnostics Reference: https://github.com/hashicorp/terraform-plugin-framework-validators/issues/57 Reference: https://github.com/hashicorp/terraform/issues/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. --- .changelog/pending.txt | 3 +++ float64validator/type_validation.go | 2 +- helpers/validatordiag/diag.go | 9 +++++---- metavalidator/all_test.go | 4 ++-- metavalidator/any_test.go | 10 +++++----- metavalidator/any_with_all_warnings_test.go | 10 +++++----- stringvalidator/type_validation.go | 2 +- 7 files changed, 22 insertions(+), 18 deletions(-) create mode 100644 .changelog/pending.txt diff --git a/.changelog/pending.txt b/.changelog/pending.txt new file mode 100644 index 00000000..3fd72861 --- /dev/null +++ b/.changelog/pending.txt @@ -0,0 +1,3 @@ +```release-note:bug +all: Included missing attribute path details in error diagnostics since they are currently not output by Terraform +``` diff --git a/float64validator/type_validation.go b/float64validator/type_validation.go index d02e2183..ae7e9dd0 100644 --- a/float64validator/type_validation.go +++ b/float64validator/type_validation.go @@ -14,7 +14,7 @@ func validateFloat(ctx context.Context, request tfsdk.ValidateAttributeRequest, if t != types.Float64Type { response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic( request.AttributePath, - "Expected value of type float64", + "expected value of type float64", t.String(), )) return 0.0, false diff --git a/helpers/validatordiag/diag.go b/helpers/validatordiag/diag.go index ca323fc9..66db1417 100644 --- a/helpers/validatordiag/diag.go +++ b/helpers/validatordiag/diag.go @@ -1,6 +1,7 @@ package validatordiag import ( + "fmt" "unicode" "unicode/utf8" @@ -13,7 +14,7 @@ func InvalidAttributeValueDiagnostic(path path.Path, description string, value s return diag.NewAttributeErrorDiagnostic( path, "Invalid Attribute Value", - capitalize(description)+", got: "+value, + fmt.Sprintf("Attribute %s %s, got: %s", path, description, value), ) } @@ -22,7 +23,7 @@ func InvalidAttributeValueLengthDiagnostic(path path.Path, description string, v return diag.NewAttributeErrorDiagnostic( path, "Invalid Attribute Value Length", - capitalize(description)+", got: "+value, + fmt.Sprintf("Attribute %s %s, got: %s", path, description, value), ) } @@ -31,7 +32,7 @@ func InvalidAttributeValueMatchDiagnostic(path path.Path, description string, va return diag.NewAttributeErrorDiagnostic( path, "Invalid Attribute Value Match", - capitalize(description)+", got: "+value, + fmt.Sprintf("Attribute %s %s, got: %s", path, description, value), ) } @@ -49,7 +50,7 @@ func InvalidAttributeTypeDiagnostic(path path.Path, description string, value st return diag.NewAttributeErrorDiagnostic( path, "Invalid Attribute Type", - capitalize(description)+", got: "+value, + fmt.Sprintf("Attribute %s %s, got: %s", path, description, value), ) } diff --git a/metavalidator/all_test.go b/metavalidator/all_test.go index fa0bef57..6203572f 100644 --- a/metavalidator/all_test.go +++ b/metavalidator/all_test.go @@ -36,7 +36,7 @@ func TestAllValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Type", - "Expected value of type string, got: types.Int64Type", + "Attribute test expected value of type string, got: types.Int64Type", ), }, }, @@ -51,7 +51,7 @@ func TestAllValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 5, got: 3", + "Attribute test string length must be at least 5, got: 3", ), }, }, diff --git a/metavalidator/any_test.go b/metavalidator/any_test.go index 29dc48e3..809992a3 100644 --- a/metavalidator/any_test.go +++ b/metavalidator/any_test.go @@ -36,7 +36,7 @@ func TestAnyValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Type", - "Expected value of type string, got: types.Int64Type", + "Attribute test expected value of type string, got: types.Int64Type", ), }, }, @@ -51,12 +51,12 @@ func TestAnyValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 4, got: 3", + "Attribute test string length must be at least 4, got: 3", ), diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 5, got: 3", + "Attribute test string length must be at least 5, got: 3", ), }, }, @@ -80,12 +80,12 @@ func TestAnyValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 6, got: 3", + "Attribute test string length must be at least 6, got: 3", ), diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 5, got: 3", + "Attribute test string length must be at least 5, got: 3", ), }, }, diff --git a/metavalidator/any_with_all_warnings_test.go b/metavalidator/any_with_all_warnings_test.go index 1707a89b..2a1044f7 100644 --- a/metavalidator/any_with_all_warnings_test.go +++ b/metavalidator/any_with_all_warnings_test.go @@ -55,7 +55,7 @@ func TestAnyWithAllWarningsValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Type", - "Expected value of type string, got: types.Int64Type", + "Attribute test expected value of type string, got: types.Int64Type", ), }, }, @@ -70,12 +70,12 @@ func TestAnyWithAllWarningsValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 4, got: 3", + "Attribute test string length must be at least 4, got: 3", ), diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 5, got: 3", + "Attribute test string length must be at least 5, got: 3", ), }, }, @@ -99,12 +99,12 @@ func TestAnyWithAllWarningsValidator(t *testing.T) { diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 6, got: 3", + "Attribute test string length must be at least 6, got: 3", ), diag.NewAttributeErrorDiagnostic( path.Root("test"), "Invalid Attribute Value Length", - "String length must be at least 5, got: 3", + "Attribute test string length must be at least 5, got: 3", ), }, }, diff --git a/stringvalidator/type_validation.go b/stringvalidator/type_validation.go index 8b8ecf2b..ab3adf9d 100644 --- a/stringvalidator/type_validation.go +++ b/stringvalidator/type_validation.go @@ -14,7 +14,7 @@ func validateString(ctx context.Context, request tfsdk.ValidateAttributeRequest, if t != types.StringType { response.Diagnostics.Append(validatordiag.InvalidAttributeTypeDiagnostic( request.AttributePath, - "Expected value of type string", + "expected value of type string", t.String(), )) return "", false From 3e289add410c161b339a9b9e56602554a1e18c08 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Sun, 28 Aug 2022 15:14:14 -0400 Subject: [PATCH 2/2] Update CHANGELOG for #61 --- .changelog/{pending.txt => 61.txt} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .changelog/{pending.txt => 61.txt} (100%) diff --git a/.changelog/pending.txt b/.changelog/61.txt similarity index 100% rename from .changelog/pending.txt rename to .changelog/61.txt