Parameter validation - by edi89

Status : 

 


5
0
Sign in
to vote
ID 1117495 Comments
Status Active Workarounds
Type Bug Repros 1
Opened 2/5/2015 11:52:27 PM
Access Restriction Public

Description

Using paramter validation in functions works just find - something like this:
function test{
    Param (
        [ValidateRange(3,8)]
        [Int] $mytest )

    write-host $mytest
}
test 4
test 9
The problem is, that it seems, that parameter validation is in fact just a special case of variable validation which leads to the circumstance, that the variable used (in this case $mytest) is beeing validated every time you use it. Try this:
function test{
    Param    (
        [ValidateRange(3,8)]
        [Int] $mytest)

    $mytest = $mytest+5
    write-host $mytest
}

test 4

It seems to be by design, but the documentation about parameter validation is misleading if not completly wrong, because it says something like:"parameters are validated when you call the function/cmdlet", which simply is not the way it works - the variable is validated everytime you use it.
Sign in to post a comment.