Skip to content
Sarj Code Standards

no-redundant-variable-validation

Flag Terraform variable validations already guaranteed by the declared type or nullability.

Why

Terraform applies declared type and nullability constraints before custom validation. Rechecking readability, conversion to the declared type, non-nullability, or an exhaustive non-nullable boolean domain cannot reject another valid input and obscures actual domain requirements.

Fix

Delete the redundant validation block. Keep validation for narrower domain requirements such as ranges, formats, enums, otherwise permitted nulls, 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."
}
}
Autofix output
variables.tf
variable "region" {
type = string
}
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."
}
}