Moving Parameter Validation in PowerShell to Private Functions

While presenting one of my presentations at the PowerShell + DevOps Global Summit last week, I demonstrated why you wouldn’t want to use ValidatePattern for parameter validation because of the useless error message that it returns when the input doesn’t match the regular expression that’s being used for validation.

I then demonstrated how ValidateScript could be used to build a better ValidatePattern. I have an older blog article that details this process if that’s something you’re interested in learning more about.

A great question was asked during this presentation: Can you use a function for parameter validation instead of having some complicated script embedded within your function that’s performing the validation? Just to be clear, you don’t want your code to continue on a path that it can’t possible complete successfully any further than necessary, which is one of the reasons you want to use parameter validation and not write custom validation within the body of the function itself. Another reason to use the parameter validation attributes is so your validation is at least similar to the validation that others would write.

With that said, if you’re going to write custom code inside of the ValidateScript block, I think that breaking it out into private functions is a great idea because of several reasons. You could reuse the same validation such as the regular expression in the previous example to validate the input of numerous functions while only having to write the code once (there’s no sense in writing redundant code) and if you ever have to update the code that’s performing the validation, there’s only one copy to update instead of trying to find all of the places you used it.

A private function could be written similar to the following for the example shown in this blog article.

And then it could be called within the ValidateScript block to achieve the same result as the first function shown in this blog article.

I’ve also created a companion video for this blog article to show what I’m talking about.

This really isn’t any different than using built-in commands such as Test-Path within the ValidateScript block to validate a user is providing an existing and valid path as input for a parameter. You wouldn’t rewrite all of the code for Test-Path within the ValidateScript block every time you wanted to validate a path.

I’d love to hear your thoughts on this topic. Please post them as a comment to this blog article. Also, let me know what you think about having short companion videos to accompany my blog articles.

µ

3 Comments

  1. jkavanagh58

    Looks cool, and if you do that often in your scripts a function would be handy but in general you are just moving the complexity of the script to a function… I just rely on a snippet but this very interesting

    Reply
  2. Piotr

    Thank you for the informative post.
    Is there a way to reference an ‘earlier’ parameter from the same function within the validation of another parameter?

    Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: