You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ValidateSet should support a dynamically generated set #3744
Staffan Gustafsson (@powercode) IntelliSense can call attributes very often. ValidateSet with script block becomes similar to ValidateScript (can run unpredictably long).
Perhaps we should do only IValidateSetGenerator (not script block) because we want support interfaces in PowerShell classes.
Also could we add IValidateSetGenerator not only in ValidateSet attribute: ValidateScript, ValidatePattern, ... for IntelliSense support?
Ilya (@iSazonov) I don't think that would be much of a problem. Most people know not to have long-running commands in intellisense code. People are already working around this issue using the less-friendly dynamicparam block and passing a ValidateSetAttribute to the attribute collection.
I feel the problem with using an interface for this is that a lot of PowerShell coders aren't programmers. It's one thing to support classes, it's another thing to force the user to use them. Just my opinion ofc. The 'PowerShell way' here is to support a ScriptBlock.
It looks like it is from DynamicParams but it is not related directly. Also we have RFC for Dynamic Keywords so we should avoid "Dynamic" term to eliminate confusion.
The example in the docs looks nothing like the example the OP of this issue wanted. The OP asks for a script block, and the solution provided is a full on syntax-verbose class.
I can't get an example like the script block to work. Was that implemented or only the syntax-verbose class version?
If not, are there any plans to support the significantly more terse syntax? Should another issue be opened?
Josh Olson (@OlsonDev) The issue description is misleading. We only implemented a variant based on IValidateSetValuesGenerator interface until we receive more feedback. Feel free open new issue with the description of the necessary feature.
Indeed. It should be possible to borrow portions of the code from [ArgumentCompleter()] for this purpose; it also takes a script block and apparently applies it to the method defined in IArgumentCompleter
Rain Sallow (/u/ta11ow) (@vexx32) There is samples in attributes.cs file how to use execution context. I assume that the tests will be most of this work and not the code itself. :-)
Activity
lfshr commentedon May 9, 2017
This is a much better solution. Well played Jason Shirk (@lzybkr) like it!
lzybkr commentedon May 9, 2017
We should also consider binary cmdlets. One option is to accept a type that implements an interface, something like:
This could probably work in PowerShell too (the interface would be defined in System.Management.Automation.dll.
I think we'd need a way of passing
$PSBoundParametersto truly cover most of the scenarios where people are resorting to dynamic parameters today.iSazonov commentedon May 9, 2017
Recently I played with this and it seems like it might be too expensive due to IntelliSense.
powercode commentedon May 9, 2017
Ilya (@iSazonov) What is to expensive?
iSazonov commentedon May 10, 2017
Staffan Gustafsson (@powercode) IntelliSense can call attributes very often. ValidateSet with script block becomes similar to ValidateScript (can run unpredictably long).
Perhaps we should do only IValidateSetGenerator (not script block) because we want support interfaces in PowerShell classes.
Also could we add IValidateSetGenerator not only in ValidateSet attribute: ValidateScript, ValidatePattern, ... for IntelliSense support?
lfshr commentedon May 10, 2017
Ilya (@iSazonov) I don't think that would be much of a problem. Most people know not to have long-running commands in intellisense code. People are already working around this issue using the less-friendly dynamicparam block and passing a ValidateSetAttribute to the attribute collection.
I feel the problem with using an interface for this is that a lot of PowerShell coders aren't programmers. It's one thing to support classes, it's another thing to force the user to use them. Just my opinion ofc. The 'PowerShell way' here is to support a ScriptBlock.
Jason Shirk (@lzybkr) would the following not be easier:
and just have a ValidateSet overload that takes in an IValidateSetGenerator, rather than using typeof?
lfshr commentedon May 10, 2017
So two new overloads for ValidateSetAttribute:
and
iSazonov commentedon May 10, 2017
IValidateSetGenerator vs IValidateSetEnumerator ?
lfshr commentedon May 10, 2017
Ilya (@iSazonov) maybe even IDynamicValidateSet?
Edit: Upon reflection I prefer IValidateSetEnumerator
iSazonov commentedon May 10, 2017
It looks like it is from DynamicParams but it is not related directly. Also we have RFC for Dynamic Keywords so we should avoid "Dynamic" term to eliminate confusion.
lzybkr commentedon May 10, 2017
Liam (@lfshr) - C# requires
typeof(MyValidateSetGenerator)becauseMyValidateSetGeneratoris not an expression.The interface name should probably not end in
Enumeratorbecause the interface does not define an enumerator.iSazonov commentedon May 10, 2017
Maybe IValidateSetValuesGetter
3 remaining items
oising commentedon May 31, 2017
Is this still up for grabs? I'm looking for something small to start with.
update: never mind, I see the PR is in progress :)
iSazonov commentedon Jun 1, 2017
Welcome to code review!
lordisp commentedon Nov 3, 2018
maybe you can help me out with my challange. I like to set ValidateSet to run my "example.ps1 -region eu-cental-1" and get the AWS regions
Eg:
Param([parameter(Mandatory=$true)][ValidateSet((Get-AWSRegion).region)][String[]]$region)process {# do whatever}vexx32 commentedon Nov 3, 2018
I don't see any link to the relevant PR, but it appears to need a script block, so you would write:
lordisp commentedon Nov 3, 2018
you're right; it's not a PR; just a little script to automate my AWS stuff. Maybe I open one once it has some relevant features.
this does not make any difference unfortunately
vexx32 commentedon Nov 3, 2018
Jason Shirk (@lzybkr) a PR is mentioned in this thread, but I don't see any links to it. Did this end up getting implemented?
iSazonov commentedon Nov 4, 2018
You need to implement
IValidateSetValuesGenerator. See:https://github.com/iSazonov/PowerShell/blob/65349de2b25022092213d86cfc1f5dfe0c92cbc6/test/powershell/Language/Classes/Scripting.Classes.Attributes.Tests.ps1#L273
vexx32 commentedon Nov 4, 2018
@rafaelc81 see here for the example that was merged into documentation after the PR was merged.
https://github.com/PowerShell/PowerShell-Docs/pull/3133/files?short_path=8b392e6#diff-8b392e69e97629458a3c80ee54386057
OlsonDev commentedon Jan 14, 2019
The example in the docs looks nothing like the example the OP of this issue wanted. The OP asks for a script block, and the solution provided is a full on syntax-verbose class.
I can't get an example like the script block to work. Was that implemented or only the syntax-verbose class version?
If not, are there any plans to support the significantly more terse syntax? Should another issue be opened?
Thanks!
iSazonov commentedon Jan 15, 2019
Josh Olson (@OlsonDev) The issue description is misleading. We only implemented a variant based on
IValidateSetValuesGeneratorinterface until we receive more feedback. Feel free open new issue with the description of the necessary feature.vexx32 commentedon Jan 15, 2019
Indeed. It should be possible to borrow portions of the code from
[ArgumentCompleter()]for this purpose; it also takes a script block and apparently applies it to the method defined inIArgumentCompleteriSazonov commentedon Jan 15, 2019
Rain Sallow (/u/ta11ow) (@vexx32) There is samples in attributes.cs file how to use execution context. I assume that the tests will be most of this work and not the code itself. :-)
OlsonDev commentedon Jan 15, 2019
Ilya (@iSazonov) Rain Sallow (/u/ta11ow) (@vexx32) Thanks, I created the aforementioned issue here.