Skip to content

Error is wrong when value given for IValidateSetValuesGenerator is not in GetValidValues() #4698

@lfshr

Description

@lfshr

Steps to reproduce

class ValidateSetTest : System.Management.Automation.IValidateSetValuesGenerator
{
    [string[]] GetValidValues()
    {
        return 'Hello', 'World'
    }
}

function Test-ValidateSet
{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true)]
        [ValidateSet([ValidateSetTest])]
        [string[]]
        $Item
    )
    $Item
}

Test-ValidateSet -Item 'Cheese'

Expected behavior

Test-ValidateSet : Cannot validate argument on parameter 'Item'. The argument "Cheese" does not belong to the set
"Hello,World" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command
again.
At line:1 char:24
+ Test-ValidateSet -Item 'Cheese'
+                        ~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-ValidateSet], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-ValidateSet

Actual behavior

Test-ValidateSet : Cannot validate argument on parameter 'Item'. Value cannot be null.
Parameter name: value
At line:1 char:24
+ Test-ValidateSet -Item 'Cheese'
+                        ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Test-ValidateSet], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Test-ValidateSet

Environment data

Name                           Value
----                           -----
PSVersion                      6.0.0-beta
PSEdition                      Core
GitCommitId                    v6.0.0-beta.6
OS                             Microsoft Windows 10.0.15063
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions