Skip to content

Add the attribute from the request to at-least-one validator's error message #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/unreleased/BUG FIXES-20240306-234246.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: Add the attribute from the request to the at-least-one validator's error message
time: 2024-03-06T23:42:46.773751412-05:00
custom:
Issue: "199"
4 changes: 4 additions & 0 deletions internal/schemavalidator/at_least_one_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ func (av AtLeastOneOfValidator) Validate(ctx context.Context, req AtLeastOneOfVa
}
}

// This attribute is among those required attributes,
// append it to make it appears in the error message.
expressions.Append(req.PathExpression)

res.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("At least one attribute out of %s must be specified", expressions),
Expand Down
55 changes: 40 additions & 15 deletions internal/schemavalidator/at_least_one_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
Expand All @@ -20,9 +22,9 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
t.Parallel()

type testCase struct {
req schemavalidator.AtLeastOneOfValidatorRequest
in path.Expressions
expErrors int
req schemavalidator.AtLeastOneOfValidatorRequest
in path.Expressions
expected *schemavalidator.AtLeastOneOfValidatorResponse
}

testCases := map[string]testCase{
Expand Down Expand Up @@ -52,6 +54,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
in: path.Expressions{
path.MatchRoot("foo"),
},
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
},
"self-is-null": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
Expand Down Expand Up @@ -79,6 +82,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
in: path.Expressions{
path.MatchRoot("foo"),
},
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
},
"error_none-set": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
Expand Down Expand Up @@ -110,7 +114,17 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
path.MatchRoot("foo"),
path.MatchRoot("baz"),
},
expErrors: 1,
expected: &schemavalidator.AtLeastOneOfValidatorResponse{
Diagnostics: diag.Diagnostics{
diag.WithPath(
path.Root("bar"),
diag.NewErrorDiagnostic(
"Invalid Attribute Combination",
"At least one attribute out of [foo,baz,bar] must be specified",
),
),
},
},
},
"multiple-set": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
Expand Down Expand Up @@ -142,6 +156,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
path.MatchRoot("foo"),
path.MatchRoot("baz"),
},
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
},
"allow-duplicate-input": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
Expand Down Expand Up @@ -174,6 +189,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
path.MatchRoot("bar"),
path.MatchRoot("baz"),
},
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
},
"unknowns": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
Expand Down Expand Up @@ -205,6 +221,7 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
path.MatchRoot("foo"),
path.MatchRoot("baz"),
},
expected: &schemavalidator.AtLeastOneOfValidatorResponse{},
},
"matches-no-attribute-in-schema": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
Expand Down Expand Up @@ -232,7 +249,23 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
in: path.Expressions{
path.MatchRoot("fooz"),
},
expErrors: 2,
expected: &schemavalidator.AtLeastOneOfValidatorResponse{
Diagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"Invalid Path Expression for Schema",
"The Terraform Provider unexpectedly provided a path expression that does not match the current schema. "+
"This can happen if the path expression does not correctly follow the schema in structure or types. "+
"Please report this to the provider developers.\n\nPath Expression: fooz",
),
diag.WithPath(
path.Root("bar"),
diag.NewErrorDiagnostic(
"Invalid Attribute Combination",
"At least one attribute out of [fooz,bar] must be specified",
),
),
},
},
},
}

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

if test.expErrors > 0 && !res.Diagnostics.HasError() {
t.Fatal("expected error(s), got none")
}

if test.expErrors > 0 && test.expErrors != res.Diagnostics.ErrorsCount() {
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, res.Diagnostics.ErrorsCount(), res.Diagnostics)
}

if test.expErrors == 0 && res.Diagnostics.HasError() {
t.Fatalf("expected no error(s), got %d: %v", res.Diagnostics.ErrorsCount(), res.Diagnostics)
if diff := cmp.Diff(test.expected, res); diff != "" {
t.Errorf("unexpected diff: %s", diff)
}
})
}
Expand Down