@@ -78,6 +78,94 @@ $ docker buildx bake --print
7878}
7979```
8080
81+ ## Validating variables
82+
83+ To verify that the value of a variable conforms to an expected type, value
84+ range, or other condition, you can define custom validation rules using the
85+ ` validation ` block.
86+
87+ In the following example, validation is used to enforce a numeric constraint on
88+ a variable value; the ` PORT ` variable must be 1024 or higher.
89+
90+ ``` hcl
91+ # Define a variable `PORT` with a default value and a validation rule
92+ variable "PORT" {
93+ default = 3000 # Default value assigned to `PORT`
94+
95+ # Validation block to ensure `PORT` is a valid number within the acceptable range
96+ validation {
97+ condition = PORT >= 1024 # Ensure `PORT` is at least 1024
98+ error_message = "The variable 'PORT' must be 1024 or higher." # Error message for invalid values
99+ }
100+ }
101+ ```
102+
103+ If the ` condition ` expression evaluates to ` false ` , the variable value is
104+ considered invalid, whereby the build invocation fails and ` error_message ` is
105+ emitted. For example, if ` PORT=443 ` , the condition evaluates to ` false ` , and
106+ the error is raised.
107+
108+ Values are coerced into the expected type before the validation is set. This
109+ ensures that any overrides set with environment variables work as expected.
110+
111+ ### Validate multiple conditions
112+
113+ To evaluate more than one condition, define multiple ` validation ` blocks for
114+ the variable. All conditions must be ` true ` .
115+
116+ Here’s an example:
117+
118+ ``` hcl
119+ # Define a variable `VAR` with multiple validation rules
120+ variable "VAR" {
121+ # First validation block: Ensure the variable is not empty
122+ validation {
123+ condition = VAR != ""
124+ error_message = "The variable 'VAR' must not be empty."
125+ }
126+
127+ # Second validation block: Ensure the value contains only alphanumeric characters
128+ validation {
129+ # VAR and the regex match must be identical:
130+ condition = VAR == regex("[a-zA-Z0-9]+", VAR)
131+ error_message = "The variable 'VAR' can only contain letters and numbers."
132+ }
133+ }
134+ ```
135+
136+ This example enforces:
137+
138+ - The variable must not be empty.
139+ - The variable must match a specific character set.
140+
141+ For invalid inputs like ` VAR="hello@world" ` , the validation would fail.
142+
143+ ### Validating variable dependencies
144+
145+ You can reference other Bake variables in your condition expression, enabling
146+ validations that enforce dependencies between variables. This ensures that
147+ dependent variables are set correctly before proceeding.
148+
149+ Here’s an example:
150+
151+ ``` hcl
152+ # Define a variable `FOO`
153+ variable "FOO" {}
154+
155+ # Define a variable `BAR` with a validation rule that references `FOO`
156+ variable "BAR" {
157+ # Validation block to ensure `FOO` is set if `BAR` is used
158+ validation {
159+ condition = FOO != "" # Check if `FOO` is not an empty string
160+ error_message = "The variable 'BAR' requires 'FOO' to be set."
161+ }
162+ }
163+ ```
164+
165+ This configuration ensures that the ` BAR ` variable can only be used if ` FOO `
166+ has been assigned a non-empty value. Attempting to build without setting ` FOO `
167+ will trigger the validation error.
168+
81169## Escape variable interpolation
82170
83171If you want to bypass variable interpolation when parsing the Bake definition,
0 commit comments