-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
XOR operation to describe validation rules for mutually-exclusive attributes #30571
Comments
Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again! |
I remember back when we were considering which operators to make builtins we had intentionally made the "power of" operation be a function rather than an operator both because it seems to be infrequently needed in Terraform (it's not an environment intended for mathematical work) and because the meaning of I think the same argument holds here too: XOR seems like an operation that is occasionally useful but not frequently used, and of course the ambiguity with Given that, I would suggest that if we do support it then we should do so using a function named |
Incidentally, I think the |
I like this idea as well! Makes sense if it is easier to implement, and it sounds something doable.
Thanks for the suggestion! It definitely makes the code more readable! |
Any news regarding this proposal? |
I agree that adding XOR might be useful but also NOT-EQUAL operator might be used comparing BOOLEAN values. |
currently working on a validation rule where this would make things MUCH cleaner. |
Thanks for sharing that use-case, @aRustyDev. I guess if we were to solve that using an
XOR is an operation on boolean values, so just using It seems technically plausible to also implement a function that takes a set of values and returns true only if exactly one of them is non-null, although it's debatable whether a more specialized function would be necessary if we had an (We are intending to implement something like #2771 in future to allow provider plugins to contribute additional functions to Terraform -- design and research for that is underway -- so at that point it'll be possible to implement more specialized functions yourself if you wish, although personally I think |
A hacky alternative that does scale nicely with larger sets, is to convert the checks to numbers and sum them, then checking the total is 1. E.g. variable "example" {
type = object({
first = optional(string)
second = optional(string)
third = optional(string)
})
validation {
condition = 1 == sum([for c in [
# add mutually exclusive values here
var.example["first"] != null,
var.example["second"] != null,
var.example["third"] != null,
] : c ? 1 : 0])
error_message = "Only one of first, second and third must be set."
}
} or even shorter condition = 1 == sum([for x in ["first", "second", "third"] : var.example[x] != null ? 1 : 0]) |
Thank you for your continued interest in this issue. Terraform version 1.8 launches with support of provider-defined functions. It is now possible to implement your own functions! We would love to see this implemented as a provider-defined function. Please see the provider-defined functions documentation to learn how to implement functions in your providers. If you are new to provider development, learn how to create a new provider with the Terraform Plugin Framework. If you have any questions, please visit the Terraform Plugin Development category in our official forum. We hope this feature unblocks future function development and provides more flexibility for the Terraform community. Thank you for your continued support of Terraform! |
I have always felt that Terraform lacks for some sort of xor operator. I also think that a It is a functionality that is so often needed in validation rules that I feel having to rely on an external provider to implement it, or hack your way around it every time is very cumbersome, while the code to add it would be very simple. (I'd be happy to implement it, in case you agree that it would be a valuable addition to Terraform.) |
Current Terraform Version
Use-cases
I am using the
optional
keyword to define an object with two properties, which are both optional, but in which at least one must be populated.For doing so, I am using
validation
to enforce this condition. While it is possible, aXOR
operator would make it way easier. Especially 'cause the workaround explodes in complexity when we are talking about multiple keys to check, and not just two.Attempted Solutions
Proposal
A
XOR
operator, in this example,^
,The text was updated successfully, but these errors were encountered: