Validation block over multiple values

This validation block works for a single input variable.

variable "mytestname" {

     validation {
        condition = length(regexall("^test", var.mytestname)) > 0
        error_message = "Should start with 'test'"
     }
}

I need it to work inside a for_each - or have some workaround to accomplish this. The issue is that there is a restriction on the condition statement - the condition HAS to take in the input variable itself (i.e. - it cannot accept an each.value)

variable "mytestnames" {

listnames = split(",",var.mytestnames)     

for_each = var.listnames

     validation {
        condition = length(regexall("^test", each.value)) > 0
        error_message = "Should start with test"
      }
}

The above snippet does not work. I need a way I can iterate over a list of values and validate each of them using the validation block.

I’d also like this feature. I’ve submitted a feature request here: https://github.com/hashicorp/terraform/issues/25648

With the feature as it’s designed today, you can get a result similar to this by using a for expression in the condition argument:

  validation {
    condition     = can([for v in var.mytestnames : regex("^test", v)])
    error_message = "All test names must have the prefix \"test\"."
  }

What this does not allow is mentioning in the error message specifically which of the items in the list were invalid, so there’s definitely room for improving the resulting error messages with future improvements to the validation feature but you can at least implement the validation rule itself today.