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 4 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
61 changes: 58 additions & 3 deletions internal/schemavalidator/at_least_one_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ package schemavalidator_test

import (
"context"
"strings"
"testing"

"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,10 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
t.Parallel()

type testCase struct {
req schemavalidator.AtLeastOneOfValidatorRequest
in path.Expressions
expErrors int
req schemavalidator.AtLeastOneOfValidatorRequest
in path.Expressions
expErrors int
expDiagsMsgSegs []string
}

testCases := map[string]testCase{
Expand Down Expand Up @@ -112,6 +115,38 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
},
expErrors: 1,
},
"both-attrs-in-diag-msg": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
ConfigValue: types.NumberNull(),
Path: path.Root("bar"),
PathExpression: path.MatchRoot("bar"),
Config: tfsdk.Config{
Schema: schema.Schema{
Attributes: map[string]schema.Attribute{
"foo": schema.Int64Attribute{},
"bar": schema.Int64Attribute{},
},
},
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"foo": tftypes.Number,
"bar": tftypes.Number,
},
},
map[string]tftypes.Value{
"foo": tftypes.NewValue(tftypes.Number, nil),
"bar": tftypes.NewValue(tftypes.Number, nil),
},
),
},
},
in: path.Expressions{
path.MatchRoot("foo"),
},
expErrors: 1,
expDiagsMsgSegs: []string{"foo", "bar"},
},
"multiple-set": {
req: schemavalidator.AtLeastOneOfValidatorRequest{
ConfigValue: types.StringValue("bar value"),
Expand Down Expand Up @@ -236,6 +271,22 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
},
}

checkDiagsForExpectedMessageSegments := func(diags diag.Diagnostics, segs []string) {
for _, seg := range segs {
found := false
for _, diag := range diags {
if strings.Contains(diag.Summary(), seg) || strings.Contains(diag.Detail(), seg) {
found = true
break
}
}

if !found {
t.Fatalf("expected diagnostic message to contain %q, got none", seg)
}
}
}

for name, test := range testCases {
name, test := name, test
t.Run(name, func(t *testing.T) {
Expand All @@ -254,6 +305,10 @@ func TestAtLeastOneOfValidatorValidate(t *testing.T) {
t.Fatalf("expected %d error(s), got %d: %v", test.expErrors, res.Diagnostics.ErrorsCount(), res.Diagnostics)
}

if len(test.expDiagsMsgSegs) > 0 {
checkDiagsForExpectedMessageSegments(res.Diagnostics, test.expDiagsMsgSegs)
}

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