-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Description
This is a follow-up to #3630.
Related: #3765
Thanks to @powercode's efforts in #2728, , we now have an optional ErrorMessage property in the ValidatePattern and ValidateScript parameter validation attributes. However, all validation attributes (Validation*; all that derive from System.Automation.Management.ValidateArgumentsAttribute) should support a custom error message:
-
Even for the simplest validations it can sometimes be helpful to provide domain-specific guidance rather than reporting a purely technical violation (see example below).
-
Consistent support eliminates the burden of having to remember which specific attributes support the property (though IntelliSense may ease that pain).
Example
function foo {
param(
[ValidateCount(2, [int]::maxvalue, ErrorMessage='This bar has a 2-drink minimum.')]
[string] $bar
)
$bar
}
foo -bar 'mint julep'Desired behavior
foo : Cannot validate argument on parameter 'bar'. This bar has a 2-drink minimum.
...
As in the implementation for ValidatePattern and ValidateScript, placeholder {0} would represent the value passed by the user, and {1}, ... the validation-attribute arguments.