Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

IMHO, incorrect PowerShell behavior when dealing with *validation*attributes*! The *validation*attributes* execution is SKIPPED when a function's parameter gets no argument.

$
0
0
function f{
[cmdletbinding()]param(
    [ValidatePattern("^[AB][CD][EF]$")]
    [ValidateNotNullOrEmpty()]
    [text.stringbuilder]$Data
)

'--- function f'
"Data is `$null: $($Data-eq$null)""Data=#$Data#""--- end of f.`n`n"
}


cls

f -ErrorAction Stop

If the validation attributes were executed for the unspecified parameter (*), then, both validation attributes would fail and the function body wouldnot be executed. Unfortunately, it is executed. The code above results in this output:

<output>
--- function f
Data is $null: True
Data=##
--- end of f.</output>

So, it's necessary to check again (inside function's body) the argument's validity (specifying the parameter as mandatory is not desirable in many instances).

(*) IMHO, an unspecified argument should get the default value for the specific type and should be submitted to the validation attributes. Validation attributes should always be'used' by the parser/interpreter/translator/whatever.

P.S.: I'm not gonna waste my time writing a bug/suggestion report because the PSTeam don't waste their time reading those writings in Connect site. I'd rather talk to Miss Pipe Line and ask to send more data through the ... pipe.


Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>