-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
contains function documentation does not clearly define how it decides whether a value is present #35414
Comments
Hi @rifelpet! Thanks for reporting this. Your input variable is defined as having type
You could make this particular case work by making the first argument to validation {
condition = contains([
tolist(["a", "b"]),
tolist(["b", "a"]),
], var.foo)
error_message = "may only contain both 'a' and 'b' strings"
} ...though I assume this was just a contrived example for easier reproduction, so applying this solution to your real configuration may require some different details. The general idea, though, is to make sure you are always comparing |
A different way to interpret this rule is to consider this variable to be a variable "foo" {
type = set(string)
default = ["a", "b"]
validation {
condition = length(setsubtract(var.foo, ["a", "b"])) == 0
error_message = "may only contain both 'a' and 'b' strings"
}
}
I hope that one of these two variations is applicable enough to your real configuration that you can apply it. Terraform seems to have behaved as intended -- the rule that two values must be of the same type to compare equal is fundamental, and Please let me know if you try one of these changes and it doesn't work, or if the solutions I've described don't seem as applicable to your real configuration as they were to the contrived reproduction case. Thanks! |
Thanks for the quick response. I understand the issue now, that The docs in both types and type-constraints do not mention the difference between lists and tuples, they're only ever used interchangeably. It does mention:
In this case, I would expect In either case, wrapping the Thanks again! |
The Automatic type conversion rules only come into play when the context allows Terraform to infer your intent. For example, the More general operators and functions like the The requirement for both operands to be of the same type is documented as part of the documentation on the Therefore I think we should resolve this issue by changing the
I'm going to label this as a documentation issue so that the relevant team can find it more easily. Thanks! |
That sounds good to me. Feel free to re-title the issue too. |
Terraform Version
Terraform Configuration Files
Debug Output
https://gist.github.com/rifelpet/387ed4c86bbf8809c19bc4c411ecdf54
Expected Behavior
The validation should have passed and the plan succeeded
Actual Behavior
The validation failed
Steps to Reproduce
terraform plan
Additional Context
The condition should evaluate to true, similar to the behavior in
terraform console
:but for some reason the validation condition is evaluating to false and triggering the validation failure. The same issue occurs if setting the variable to an explicit (valid) value via tfvars or similar.
Simplifying the variable type to just a string works as expected:
References
No response
The text was updated successfully, but these errors were encountered: