-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
config: Allow module authors to specify validation rules for variables
The existing "type" argument allows specifying a type constraint that allows for some basic validation, but often there are more constraints on a variable value than just its type. This new feature (requiring an experiment opt-in for now, while we refine it) allows specifying arbitrary validation rules for any variable which can then cause custom error messages to be returned when a caller provides an inappropriate value. variable "example" { validation { condition = var.example != "nope" error_message = "Example value must not be \"nope\"." } } The core parts of this are designed to do as little new work as possible when no validations are specified, and thus the main new checking codepath here can therefore only run when the experiment is enabled in order to permit having validations.
- Loading branch information
1 parent
cef4c3b
commit 830a065
Showing
21 changed files
with
641 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
configs/testdata/invalid-files/variable-validation-bad-msg.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
terraform { | ||
experiments = [variable_validation] | ||
} | ||
|
||
variable "validation" { | ||
validation { | ||
condition = var.validation != 4 | ||
error_message = "not four" # ERROR: Invalid validation error message | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
configs/testdata/invalid-files/variable-validation-condition-badref.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
terraform { | ||
experiments = [variable_validation] | ||
} | ||
|
||
locals { | ||
foo = 1 | ||
} | ||
|
||
variable "validation" { | ||
validation { | ||
condition = local.foo == var.validation # ERROR: Invalid reference in variable validation | ||
error_message = "Must be five." | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
configs/testdata/invalid-files/variable-validation-condition-noref.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
terraform { | ||
experiments = [variable_validation] | ||
} | ||
|
||
variable "validation" { | ||
validation { | ||
condition = true # ERROR: Invalid variable validation condition | ||
error_message = "Must be true." | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...ta/invalid-modules/variable-validation-without-optin/variable-validation-without-optin.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
variable "validation_without_optin" { | ||
validation { # ERROR: Custom variable validation is experimental | ||
condition = var.validation_without_optin != 4 | ||
error_message = "Must not be four." | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
configs/testdata/warning-files/variable_validation_experiment.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
terraform { | ||
experiments = [variable_validation] # WARNING: Experimental feature "variable_validation" is active | ||
} | ||
|
||
variable "validation" { | ||
validation { | ||
condition = var.validation == 5 | ||
error_message = "Must be five." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.