Linked ValidateSets

Welcome Forums General PowerShell Q&A Linked ValidateSets

This topic contains 5 replies, has 3 voices, and was last updated by

 
Moderator
4 months, 2 weeks ago.

  • Author
    Posts
  • #129195

    Participant
    Topics: 1
    Replies: 2
    Points: 37
    Rank: Member

    I'd like to provide validation to a script that would contain "linked" parameters validated using the ValidateSet attribute. Here's a contrived example of what I'm talking about (obviously this syntax is wrong):

    Param(
    [ValidateScript('Dog', 'Cat', 'Fish')]
    [string] $Animal,
    
    [ValidateScript('SiberianHusky', 'GoldenLab')] # Dog
    [ValidateScript('Siamese', 'MaineCoon')]       # Cat
    [ValidateScript('GoldFish', 'Beta')]           # Fish
    [string] $Breed
    )
    For example, if I select 'Dog' then I would only want to validate $Breed with 'SiberianHusky' and 'GoldenLab'. Is there an easy way of doing this or do I need to do some sort of Dynamic Parameter?
    Thanks,
    Derek
  • #129221

    Moderator
    Topics: 8
    Replies: 736
    Points: 2,059
    Helping Hand
    Rank: Community Hero

    I think, What you are referring to is [ValidateSet()] attribute. You cannot have multiple declaration of ValidateSet for a Parameter Set(here only one and is default). You can club this with [ValidateScript()] attribute like below.

    function t {
    Param(
        [ValidateSet('Dog', 'Cat', 'Fish')]
        [string] $Animal,
    
        [ValidateScript({
            $Temp = $_
            switch ($Animal){
                'Dog' {[ValidateSet('SiberianHusky', 'GoldenLab')]$Breed = $Temp;$true}
                'Cat' {[ValidateSet('Siamese', 'MaineCoon')]$Breed = $Temp;$true}
                'Fish' {[ValidateSet('GoldFish', 'Beta')]$Breed = $Temp;$true}
            }
        })]
        [string] $Breed
    )
    }
    
  • #129248

    Participant
    Topics: 1
    Replies: 2
    Points: 37
    Rank: Member

    I think this is a clean solution. Thanks @kvprasoon!

  • #129255

    Participant
    Topics: 2
    Replies: 397
    Points: 689
    Helping Hand
    Rank: Major Contributor

    ValidateScript() does however lose the ability to tab-complete possible values. One alternative is to also implement an ArgumentCompleter in order to examine any already-specified parameters.

    Additionally, I don't think you can actually reference other parameters from ValidateScript; it operates outside the function scope. You would need dynamicparam here, potentially. I suspect that ValidateScript will not be particularly effective; if you add a default switch case that throws (as anyone would if you want proper validation) it would likely throw every time.

    • #129369

      Moderator
      Topics: 8
      Replies: 736
      Points: 2,059
      Helping Hand
      Rank: Community Hero

      From what I've seen, DynamicParams doesn't get the value of the defined Parameters, its possible to check whether the parameter binding, but not the value it bound to and ValidateScript understand the parameter and its value, But it depends on the order of Binding. In above example fails for below usage

      ... -Breed Goldfish -Animal Fish
      
  • #129260

    Participant
    Topics: 1
    Replies: 2
    Points: 37
    Rank: Member

    Based on your mention of ArgumentCompleter, I found this post that looks like it accomplishes all needs:

    https://rohnspowershellblog.wordpress.com/2017/01/17/completing-parameter-values-with-other-parameter-values/

     

The topic ‘Linked ValidateSets’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort