You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
require.ErrorContains(t, e.Run(context.Background(), &ast.Call{Task: "missing-var"}), "task: Task \"missing-var\" cancelled because it is missing required variables: foo")
require.ErrorContains(t, e.Run(context.Background(), &ast.Call{Task: "validation-var", Vars: vars}), "task: Task \"validation-var\" cancelled because it is missing required variables:\n - foo has an invalid value : 'bar' (allowed values : [one two])")
Copy file name to clipboardExpand all lines: website/docs/usage.mdx
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1060,6 +1060,40 @@ tasks:
1060
1060
vars: [IMAGE_NAME, IMAGE_TAG]
1061
1061
```
1062
1062
1063
+
### Ensuring required variables have allowed values
1064
+
1065
+
If you want to ensure that a variable is set to one of a predefined set of valid values before executing a task, you can use requires.
1066
+
This is particularly useful when there are strict requirements for what values a variable can take, and you want to provide clear feedback to the user when an invalid value is detected.
1067
+
1068
+
To use `requires`, you specify an array of allowed values in the vars sub-section under requires. Task will check if the variable is set to one of the allowed values.
1069
+
If the variable does not match any of these values, the task will raise an error and stop execution.
1070
+
1071
+
This check applies both to user-defined variables and environment variables.
1072
+
1073
+
Example of using `requires`:
1074
+
1075
+
```yaml
1076
+
version: '3'
1077
+
1078
+
tasks:
1079
+
deploy:
1080
+
cmds:
1081
+
- echo "deploying to {{.ENV}}"
1082
+
1083
+
requires:
1084
+
vars:
1085
+
- name: ENV
1086
+
enum: [dev, beta, prod]
1087
+
```
1088
+
1089
+
If `ENV` is not one of 'dev', 'beta' or 'prod' an error will be raised.
1090
+
1091
+
:::note
1092
+
1093
+
This is supported only for string variables.
1094
+
1095
+
:::
1096
+
1063
1097
## Variables
1064
1098
1065
1099
Task allows you to set variables using the `vars` keyword. The following
0 commit comments