Note that there are some explanatory texts on larger screens.

plurals
  1. POPowershell - [AllowEmptyString()] not working
    primarykey
    data
    text
    <p>I have a parameter as follows:</p> <pre><code>[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true)] [AllowEmptyString()] [ValidateSet("1", "2", "3", "4", "5", "6", "Critical", "Important", "High", "Medium", "Low", "Lowest")] public string Priority { get; set; } </code></pre> <p>When I run my command with <code>-Priority ""</code>, it does not work</p> <p>I know I can just skip the argument, but the real problem for me is when I execute my command using a pipe from import-csv</p> <p>If my csv does not have any value, Import-Csv updates my parameter value to empty and so I get the following error:</p> <pre><code>Cannot validate argument on parameter 'Priority'. The argument "" does not belong to the set "1,2,3,4,5,6,Critical,Important,High,Med ium,Low,Lowest" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again. </code></pre> <p>If I extend my validation set to include "", the the command if invoked individually runs successfully, however it still does not work if I pipe it to <code>Import-Csv</code></p> <p>I have also tried <code>AllowNull</code> attribute, but still the same error</p> <p><strong>UPDATE:</strong> After discussing with Garath, I appears that</p> <ol> <li><p>AllowEmptyString does not work, instead use "" in ValidateSet</p> <p>The question still remains unanswered - why doesnt <code>[AllowEmptyString()]</code> work?</p></li> <li><p>CSV files must have trialing commas even if there are no values</p> <p>Here it seems if trailing commas are not there then <code>Import-Csv</code> passes null to the command and that fails validation given by <code>ValidateSet</code></p> <p>When trailing commas are present, it seems empty is passed to the parameter which is acceptable as the set includes ""</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload