hashicorp / hashicorp/terraform-plugin-framework-validators

Include current attribute path in validator diag message for `ExactlyOneOf`

Open
#118 0 comments 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
28
Forks
13
Avg merge
2d 13h
Merged PRs (30d)
1

Description

### Terraform CLI and Framework Versions

```
Terraform v1.3.8
on darwin_arm64

github.com/hashicorp/terraform-plugin-framework v1.1.1
```

### Use Cases or Problem Statement

Given the following resource schema + config:

```go
// example_widget Resource
resp.Schema = schema.Schema{
// ... other configuration ...

Attributes: map[string]schema.Attribute{
// ... other attributes ...

"existing_attribute": schema.StringAttribute{
Optional: true,
},
"new_attribute": schema.StringAttribute{
Optional: true,
Validators: []validator.String{
stringvalidator.ExactlyOneOf(path.Expressions{
path.MatchRoot("existing_attribute"),
}...),
},
},
},
}
```
```terraform
resource "example_widget" "example" {
existing_attribute = "there can"
new_attribute = "only be one"
}
```

Produces a validation error correctly, but does not include the path of the attribute that the validation is applied to `new_attribute`:
```bash
│ Error: Invalid Attribute Combination

│ with example_widget.example,
│ on main.tf line 11, in resource "example_widget" "example":
│ 11: new_attribute = "only be one"

│ 2 attributes specified when one (and only one) of [existing_attribute] is required
```

### Proposal

Add the path of the attribute that the validator is applied to, to the validation diag message, like:

```bash
│ Error: Invalid Attribute Combination

│ with example_widget.example,
│ on main.tf line 11, in resource "example_widget" "example":
│ 11: new_attribute = "only be one"

│ 2 attributes specified when one (and only one) of [new_attribute, existing_attribute] is required
```

### Additional Information

N/A

### Code of Conduct

- [X] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.