Skip to content

no-redundant-variable-validation

Flag Terraform variable validation conditions already guaranteed by the variable's type constraint.

Why

Terraform applies a declared type constraint before custom validation. Rechecking that the same value can be read, converted to its declared type, or exhausts a non-nullable boolean domain adds a second contract that cannot reject a valid input and obscures the validations that enforce real domain rules.

Fix

Delete the redundant validation block. Keep validation for narrower domain requirements such as ranges, formats, enums, nullability, and relationships between fields.

Examples

Before — flagged String conversion repeats the declared string type
variables.tf
variable "region" {
type = string
validation {
condition = can(tostring(var.region))
error_message = "Region must be a string."
}
}
After — preferred String validation enforces a narrower domain format
variables.tf
variable "region" {
type = string
validation {
condition = can(regex("^[a-z]+-[a-z]+[0-9]+$", var.region))
error_message = "Region must use the provider region format."
}
}
Before — flagged Boolean enumeration repeats a non-nullable boolean type
variables.tf
variable "enabled" {
type = bool
nullable = false
validation {
condition = contains([true, false], var.enabled)
error_message = "Enabled must be true or false."
}
}
After — preferred Boolean enumeration rejects an otherwise permitted null
variables.tf
variable "enabled" {
type = bool
validation {
condition = contains([true, false], var.enabled)
error_message = "Enabled must not be null."
}
}