Skip to content
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

Include current attribute path in validator diag message for ExactlyOneOf #118

Open
1 task done
austinvalle opened this issue Mar 10, 2023 · 0 comments
Open
1 task done
Labels
enhancement New feature or request

Comments

@austinvalle
Copy link
Member

austinvalle commented Mar 10, 2023

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:

// 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"),
				}...),
			},
		},
	},
}
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:

│ 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:

│ 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

  • I agree to follow this project's Code of Conduct
@austinvalle austinvalle added the enhancement New feature or request label Mar 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant