Skip to content

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

Open
@austinvalle

Description

@austinvalle

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions