diff --git a/website/content/docs/job-specification/hcl2/variables.mdx b/website/content/docs/job-specification/hcl2/variables.mdx index 943e41be6..f3d1f5ab5 100644 --- a/website/content/docs/job-specification/hcl2/variables.mdx +++ b/website/content/docs/job-specification/hcl2/variables.mdx @@ -154,6 +154,28 @@ documentation about the job, and so it should be written from the perspective of the user of the job rather than its maintainer. For commentary for job maintainers, use comments. +## Input Variable Custom Validation Rules + +Input variables support specifying arbitrary custom validation rules for a particular variable using a `validation` block nested within the corresponding `variable` block: + +```hcl +variable "image_id" { + type = string + description = "The id of the machine image (AMI) to use for the server." + + validation { + condition = length(var.image_id) > 4 && substr(var.image_id, 0, 4) == "ami-" + error_message = "The image_id value must be a valid AMI id, starting with \"ami-\"." + } +} +``` + +The condition argument is an expression that must use the value of the variable to return true if the value is valid, or false if it is invalid. The expression can refer only to the variable that the condition applies to, and _must_ not produce errors. + +If condition evaluates to false, Nomad will produce an error message that includes the sentences given in `error_message`. The error message string should be at least one full sentence explaining the constraint that failed, starting with an uppercase letter ( if the alphabet permits it ) and ending with a period or question mark. + +Multiple validation blocks can be declared in which case error messages will be returned for all failed conditions. + ## Assigning Values to job Variables Once a variable is declared in your configuration, you can set it: