How can we pass dynamic values to a custom rule? can we create a rule with parameters like we create a function with parameters? #1115
-
How can we pass dynamic values to a custom rule? can we create a rule with parameters like we create a function with parameters? I have created below rule where I am passing expected value of "$workerRuntime" as hardcoded in the rule. Is there any way I can pass this value dynamically using parameters or any external file? # Synopsis: key FUNCTIONS_WORKER_RUNTIME must have value 'dotnet'
Rule 'FUNCTIONS_WORKER_RUNTIME' {
$workerRuntime = "dotnet"
AllOf {
Exists 'ApplicationSettings.FUNCTIONS_WORKER_RUNTIME' -CaseSensitive
Within 'ApplicationSettings.FUNCTIONS_WORKER_RUNTIME' $workerRuntime -CaseSensitive
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
@ankur90Git Thanks for the question. Currently there isn't a global variable or parameters concept in PSRule, however there is an option to use configuration. Configuration works for PowerShell-based rules but we are looking at YAML and JSON rule support. This should achieve what you are looking for. You can set configuration key/values in code, file (via See Options for the full details on how to do this. You could update your rule to be something like: # Synopsis: key FUNCTIONS_WORKER_RUNTIME must have value 'dotnet'
Rule 'FUNCTIONS_WORKER_RUNTIME' {
AllOf {
Exists 'ApplicationSettings.FUNCTIONS_WORKER_RUNTIME' -CaseSensitive
Within 'ApplicationSettings.FUNCTIONS_WORKER_RUNTIME' $configuration.WORKERRUNTIME -CaseSensitive
}
} Example adding the configuration value to configuration:
WORKERRUNTIME: 'dotnet' |
Beta Was this translation helpful? Give feedback.
@ankur90Git Thanks for the question.
Currently there isn't a global variable or parameters concept in PSRule, however there is an option to use configuration. Configuration works for PowerShell-based rules but we are looking at YAML and JSON rule support. This should achieve what you are looking for.
You can set configuration key/values in code, file (via
ps-rule.yaml
), or environment variables depending on how you are running PSRule one of these options should work.See Options for the full details on how to do this.
You could update your rule to be something like: