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

Validation condition of a variable based on another variable #33632

Closed
sankara911 opened this issue Aug 4, 2023 · 4 comments
Closed

Validation condition of a variable based on another variable #33632

sankara911 opened this issue Aug 4, 2023 · 4 comments
Labels
duplicate issue closed because another issue already tracks this problem enhancement

Comments

@sankara911
Copy link

Terraform Version

terraform version
Terraform v1.3.2
on darwin_arm64

Use Cases

Validation of a variable based on another variable.
Currently, while declaring a variable there is a


variable "somevar"{
type=string
default=""

validation {
condition = var.somevar == "" || var.somevar == "hello"
error = "variable should be either null or hello"
}

But, if I use that validation to check another variable as below,


variable "somevar"{
type=string
default=""

validation {
condition = var.somevar == "" || var.someothervar == "hello"
error = "variable should be either null or hello"
}

I get the error -

The condition for variable "somevar" can only refer to the variable
│ itself, using var.somevar.


Would be a good addition to enable such a feature.

Attempted Solutions

But, if I use that validation to check another variable as below,

variable "somevar"{
type=string
default=""

validation {
condition = var.somevar == "" || var.someothervar == "hello"
error = "variable should be either null or hello"
}

I get the error -

The condition for variable "somevar" can only refer to the variable
│ itself, using var.somevar

Proposal

Proposing that a condition in a validation block be allowed to check the validation for another variable and set this one accordingly.

References

No response

@sankara911 sankara911 added enhancement new new issue not yet triaged labels Aug 4, 2023
@jbardin
Copy link
Member

jbardin commented Aug 4, 2023

Hi @sankara911,

While this is a duplicate of #25609, the overall functionality of validating multiple variables in a single expression can be done with check blocks.

Thanks!

@jbardin jbardin closed this as completed Aug 4, 2023
@jbardin jbardin added the duplicate issue closed because another issue already tracks this problem label Aug 4, 2023
@briceburg
Copy link

@jbardin AFAIK the terraform 1.5 check blocks are essentially post-apply/postcondition validation, whereas input validation is typically a pre-apply/pre-condition step. is there a way to have check blocks fire before a resource attempts application?

@jbardin
Copy link
Member

jbardin commented Aug 17, 2023

@briceburg, check blocks will be evaluated during plan, but yes, they will not block application of that plan. If you need to halt execution as early as possible, you can create a precondition at the point where the variables are used, or insert an empty resource like terraform_data to give you a location for a precondition where both variables are in scope.

@crw crw removed the new new issue not yet triaged label Aug 24, 2023
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 Dec 12, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate issue closed because another issue already tracks this problem enhancement
Projects
None yet
Development

No branches or pull requests

4 participants