Skip to content

Latest commit

 

History

History

ValidateScript-attribute

ValidateScript and cryptic error messages

The rules for a validation script are

  1. On invalid values it may return false or nothing.
  2. On invalid values it may throw an exception.
  3. On valid values it must return true.

The case 1 tends to produce cryptic error messages because all that PowerShell can do is to show the validation script itself. Such a message is often not user friendly even for relatively simple scripts.

The case 2 may be preferable because it explains the problem with a user friendly message specified on throw.

And now the rule 3 comes. A script cannot just throw an exception on invalid values. It still has to return true for valid. As a result, the approach 2 should look like { if (<$_ is valid>) {$true} else {throw <message>} }.

Scripts

ValidateScript is invoked for each item in a collection

It may be not obvious but when an input value is a collection then a script is invoked for each item in it. In many cases this is a handy feature, it just has to be kept in mind.

ValidateScript does not allow null

If null is a valid input value then ValidateScript cannot be used.


See also

  • Help about_Functions_Advanced_Parameters.