Skip to content

Commit 8663d64

Browse files
Add the attribute from the request to at-least-one validator's error message (#199)
* Append the attribute to requiring at least one attributes error message * Add test case for expected diag message * Add change log * Apply suggestions from @bendbennett Co-authored-by: Benjamin Bennett <[email protected]> * Apply the suggestion of comparing responses in the tests * Remove unnecessary test Co-authored-by: Benjamin Bennett <[email protected]> --------- Co-authored-by: Benjamin Bennett <[email protected]>
1 parent af62339 commit 8663d64

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kind: BUG FIXES
2+
body: Add the attribute from the request to the at-least-one validator's error message
3+
time: 2024-03-06T23:42:46.773751412-05:00
4+
custom:
5+
Issue: "199"

internal/schemavalidator/at_least_one_of.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ func (av AtLeastOneOfValidator) Validate(ctx context.Context, req AtLeastOneOfVa
9191
}
9292
}
9393

94+
// This attribute is among those required attributes,
95+
// append it to make it appears in the error message.
96+
expressions.Append(req.PathExpression)
97+
9498
res.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
9599
req.Path,
96100
fmt.Sprintf("At least one attribute out of %s must be specified", expressions),

internal/schemavalidator/at_least_one_of_test.go

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"context"
88
"testing"
99

10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-framework/diag"
1012
"github.com/hashicorp/terraform-plugin-framework/path"
1113
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1214
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
@@ -20,9 +22,9 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
2022
t.Parallel()
2123

2224
type testCase struct {
23-
req schemavalidator.AtLeastOneOfValidatorRequest
24-
in path.Expressions
25-
expErrors int
25+
req schemavalidator.AtLeastOneOfValidatorRequest
26+
in path.Expressions
27+
expected *schemavalidator.AtLeastOneOfValidatorResponse
2628
}
2729

2830
testCases := map[string]testCase{
@@ -52,6 +54,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
5254
in: path.Expressions{
5355
path.MatchRoot("foo"),
5456
},
57+
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
5558
},
5659
"self-is-null": {
5760
req: schemavalidator.AtLeastOneOfValidatorRequest{
@@ -79,6 +82,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
7982
in: path.Expressions{
8083
path.MatchRoot("foo"),
8184
},
85+
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
8286
},
8387
"error_none-set": {
8488
req: schemavalidator.AtLeastOneOfValidatorRequest{
@@ -110,7 +114,17 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
110114
path.MatchRoot("foo"),
111115
path.MatchRoot("baz"),
112116
},
113-
expErrors: 1,
117+
expected: &schemavalidator.AtLeastOneOfValidatorResponse{
118+
Diagnostics: diag.Diagnostics{
119+
diag.WithPath(
120+
path.Root("bar"),
121+
diag.NewErrorDiagnostic(
122+
"Invalid Attribute Combination",
123+
"At least one attribute out of [foo,baz,bar] must be specified",
124+
),
125+
),
126+
},
127+
},
114128
},
115129
"multiple-set": {
116130
req: schemavalidator.AtLeastOneOfValidatorRequest{
@@ -142,6 +156,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
142156
path.MatchRoot("foo"),
143157
path.MatchRoot("baz"),
144158
},
159+
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
145160
},
146161
"allow-duplicate-input": {
147162
req: schemavalidator.AtLeastOneOfValidatorRequest{
@@ -174,6 +189,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
174189
path.MatchRoot("bar"),
175190
path.MatchRoot("baz"),
176191
},
192+
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
177193
},
178194
"unknowns": {
179195
req: schemavalidator.AtLeastOneOfValidatorRequest{
@@ -205,6 +221,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
205221
path.MatchRoot("foo"),
206222
path.MatchRoot("baz"),
207223
},
224+
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
208225
},
209226
"matches-no-attribute-in-schema": {
210227
req: schemavalidator.AtLeastOneOfValidatorRequest{
@@ -232,7 +249,23 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
232249
in: path.Expressions{
233250
path.MatchRoot("fooz"),
234251
},
235-
expErrors: 2,
252+
expected: &schemavalidator.AtLeastOneOfValidatorResponse{
253+
Diagnostics: diag.Diagnostics{
254+
diag.NewErrorDiagnostic(
255+
"Invalid Path Expression for Schema",
256+
"The Terraform Provider unexpectedly provided a path expression that does not match the current schema. "+
257+
"This can happen if the path expression does not correctly follow the schema in structure or types. "+
258+
"Please report this to the provider developers.\n\nPath Expression: fooz",
259+
),
260+
diag.WithPath(
261+
path.Root("bar"),
262+
diag.NewErrorDiagnostic(
263+
"Invalid Attribute Combination",
264+
"At least one attribute out of [fooz,bar] must be specified",
265+
),
266+
),
267+
},
268+
},
236269
},
237270
}
238271

@@ -246,16 +279,8 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
246279
PathExpressions: test.in,
247280
}.Validate(context.TODO(), test.req, res)
248281

249-
if test.expErrors > 0 && !res.Diagnostics.HasError() {
250-
t.Fatal("expected error(s), got none")
251-
}
252-
253-
if test.expErrors > 0 && test.expErrors != res.Diagnostics.ErrorsCount() {
254-
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, res.Diagnostics.ErrorsCount(), res.Diagnostics)
255-
}
256-
257-
if test.expErrors == 0 && res.Diagnostics.HasError() {
258-
t.Fatalf("expected no error(s), got %d: %v", res.Diagnostics.ErrorsCount(), res.Diagnostics)
282+
if diff := cmp.Diff(test.expected, res); diff != "" {
283+
t.Errorf("unexpected diff: %s", diff)
259284
}
260285
})
261286
}

0 commit comments

Comments
 (0)