- 
                Notifications
    
You must be signed in to change notification settings  - Fork 10.1k
 
Description
Terraform Version
Terraform v1.13.4
on darwin_arm64Use Cases
I'm doing a lot of input validation. However I would like to be able to not only check that validation fails by using:
expect_failures = [
    var.input,
  ]I wan to be able to pinpoint the exact cause.
The reason is that the current expect_failure does not guarantee that my test setup doesn't have a different invalid value. For complex objects with various validations this can lead to an invalid test not verifying the correct validation under test.
Attempted Solutions
I don't see a workaround.
Proposal
For a variable with complex setup and multiple validations:
variable "input" {
  type = object({
    name = string
   // anything else
  })
  validation {
    condition = var.input != null && startswith(var.input.name, " ") ])
    error_message = "Validation error in {input.name}: Must not start with a whitespace"
  }
// further validationsMaybe something like this:
expect_failures = {
    "Validation error in {input.name}: Must not start with a whitespace" = var.input
}Where the key is the expected error_message and the value is the reference.
You could add multiple entries, but only one per message.
expect_failures = {
    "Validation error in {input.name}: Must not start with a whitespace" = var.input
    "Validation error in {input.id}: is not a valid UUID" = var.input
}To solve that maybe something like:
expect_failures = {
    "Validation error in {input.name}: Must not start with a whitespace" = [
      var.input,
    ]
    "ID not a valid UUID" = [
      var.input,
      var.target,
    ]
}References
Other programming languages give you the ability to check for the message of an exception.