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

variable validation fails on optional variables #34148

Closed
ChristianRaoulis opened this issue Oct 27, 2023 · 4 comments
Closed

variable validation fails on optional variables #34148

ChristianRaoulis opened this issue Oct 27, 2023 · 4 comments
Labels
bug new new issue not yet triaged waiting-response An issue/pull request is waiting for a response from the community

Comments

@ChristianRaoulis
Copy link

Terraform Version

Terraform v1.6.2
on windows_amd64
+ provider registry.terraform.io/hashicorp/helm v2.11.0
+ provider registry.terraform.io/hashicorp/kubernetes v2.23.0

Terraform Configuration Files

variable "example" {
  type = object({
    test = optional(object({
      val = string
    }))
  })

  validation {
    condition     = var.example.test == null || length(var.example.test.val) > 0
    error_message = "Invalid"
  }
}

example = {}

Debug Output

terraform plan error: This value is null, so it does not have any attributes.

Expected Behavior

The null check of the optional attribute should end the validation successfully

Actual Behavior

It causes an NPE in length(var.example.test.val) > 0 since var.example.test is null

Steps to Reproduce

  1. terraform plan

Additional Context

No response

References

No response

@ChristianRaoulis ChristianRaoulis added bug new new issue not yet triaged labels Oct 27, 2023
@jbardin
Copy link
Member

jbardin commented Oct 27, 2023

Hi @Skyriis,

Thanks for filing the issue. This appears to only be an instance of #24128, the error is correct that you can't evaluate var.example.test.val because var.example.test is null. The current options would be to define a suitable default value in the type expression, or to change the conditional to use try when indexing attributes might fail.

I'm not sure what you mean by this causing an "NPE" however. Do you have a configuration which causes a panic and stack trace from Terraform?

Thanks!

@jbardin jbardin added the waiting-response An issue/pull request is waiting for a response from the community label Oct 27, 2023
@apparentlymart
Copy link
Contributor

Here's one way to write that condition with today's Terraform:

  validation {
    condition     = var.example.text != null ? length(var.example.test.val) > 0 : true
    error_message = "Invalid"
  }

The conditional operator only evaluates one of its result "arms" depending on the predicate result, so in the above case if var.example.text is null then Terraform will evaluate only the true result, and not try to evaluate length(var.example.test.val) > 0.

(The unchosen "arm" of the conditional is still subject to static checking, but whether the text is attribute is set to null is a dynamic decision and so will be checked only during full evaluation.)

@jbardin
Copy link
Member

jbardin commented Dec 7, 2023

Since we have not heard back in a while I'm going to close the issue; updates can be tracked via #24128. If you have any updates regarding the issue, feel free to open a new issue with the requested information. If you have more questions, you can also use the community forum where there are more people ready to help.

Thanks!

@jbardin jbardin closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2023
Copy link
Contributor

github-actions bot commented Jan 7, 2024

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 Jan 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug new new issue not yet triaged waiting-response An issue/pull request is waiting for a response from the community
Projects
None yet
Development

No branches or pull requests

3 participants