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

Pass value from validation block condition to error_message #30643

Closed
drewmullen opened this issue Mar 10, 2022 · 3 comments
Closed

Pass value from validation block condition to error_message #30643

drewmullen opened this issue Mar 10, 2022 · 3 comments
Labels
enhancement new new issue not yet triaged

Comments

@drewmullen
Copy link
Contributor

Current Terraform Version

$ Terraform v1.1.6
on darwin_amd64
+ provider registry.terraform.io/hashicorp/aws v4.4.0

Your version of Terraform is out of date! The latest version
is 1.1.7. You can update by downloading from https://www.terraform.io/downloads.html```

Use-cases

I often use nested maps as variables and perform validation for complex situations. It would be great if i could provider extra context to the error_message a users sees by passing output from the condition check

Proposal

change condition to fail if non-true, meaning false or a string. the output of condition can be used as part of the error:

In the example below i want to make sure the keys listed in var.subnets contain only one key cidrs or netmask. private would error and be passed to error message:

> var.subnets
{
  "private" = {
    "cidrs" = ["10.0.0.0/16"]
    "netmask" = 24
  }
  "public" = {
    "netmask" = 24
  }
}

validation propsal

validation {
  error_message = "Each subnet type must contain only 1 key: `cidrs` or `netmask`. Subnet type ${condition_output} violated."
  condition     = [for subnet_type, v in var.subnets: subnet_type if length(setintersection(keys(v), ["cidrs", "netmask"])) != 1]
}

error proposal:

│ Error: Invalid value for variable
│ 
│   on variables.tf line 64:
│   64: variable "subnets" {
│ 
│ Each subnet type must contain only 1 key: `cidrs` or `netmask`. Subnet type ["private"] violated.

References

@drewmullen drewmullen added enhancement new new issue not yet triaged labels Mar 10, 2022
@jbardin
Copy link
Member

jbardin commented Mar 10, 2022

Hi @drewmullen,

This seems very similar to the proposal made in #24160, which was recently closed via #30613. I'm not sure if making the condition value "truthy" wold be acceptable, but perhaps the ability to use expressions there would suffice for your needs?

@drewmullen
Copy link
Contributor Author

Hi @jbardin!

Wow. What a coincidence. Yes i do think that provides a solution to my use case. It doesnt however help with DRYness. My suggestion for "truthy" is certainly more pythonic than gopher-ic 😅 Ill close this for now... I'm going to keep thinking on this to see if i can come up with another suggestion to help with DRY

Either way I'm really looking forward to when this new experiment is available

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

2 participants