-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValidateSet with class does not work #19676
Comments
I would expect that testvalidate.psm1 would need to be in an installed module. The rules for But can be after comments and "#!/usr/bin/env pwsh" ... |
@rhubarb-geek-nz That could indeed be the issue, because when I give the command It would be nice that the I guess this will need to be a feature request then. |
You might be able solve the problem by having a function which has the the [ValidateSet] attributes for its parameters. So you define the parameters for the script at the top as typeless but with appropriate defaults, load your module then validate the parameters by calling your function which you define after loading the module. |
@rhubarb-geek-nz But then I will loose the tab-completion on that parameter, right? |
I would expect so. For tab completion to work I expect it needs to have access to all the classes to do the value matching. If all the command line interpreter does is read the param stanza then it won't have enough information. I don't expect that tab-completion is done by actually running the script. about_Functions_Argument_Completion Can you not do something like their example?
The auto-completion offered both 'hello' and 'world' as options This also errors when you try and assign 'bye' to $Message |
@rhubarb-geek-nz No, I cannot use a static set, as the validation set is actually dynamic, based on configuration files that might change. I do not want to copy the values from those files into the script due to maintenance. I just used the simple example to show what is going wrong. |
Your symptom is a variation of a long-standing, fundamental problem that appears to be a distinct bug:
As an aside: Note that neither There's also a meta issue tracking all |
Are the configuration values so dynamic that they could change from run to run, or only from deployment to deployment? Could you write a tool to update your script before deployment with the current set of values? Could you load your class in the PowerShell profile? |
They might change from run to run
That would be possible, but that is also something to forget as a user. So for me, and also from a user perspective, this is really a no-go.
Of course that is possible, but that would mean that all shortcuts to PowerShell need to be updated on the machines that the users are using (even including Linux machines). Not a very nice scenario. |
There is a workaround, but it is somewhat cumbersome: using module ./testvalidate.psm1
param(
[Parameter(Mandatory)]
[ValidateScript({
if ($_ -notin [TestValidate]::new().GetValidValues()) { throw "Not a valid value: $_" }
$true
})]
[ArgumentCompleter({
param($command, $param, $wordToComplete)
[TestValidate]::new().GetValidValues() -like "$wordToComplete*"
})]
[string]$foo
) The unfortunate side effect of using |
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you. |
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes. |
This comment was marked as resolved.
This comment was marked as resolved.
After trying my luck in the PowerShell discord (https://discord.com/channels/180528040881815552/1221837959922192455) I found this issue through the long list of class issues. I see the issue is closed, but I don't understand why, because the issue itself still exists. I have the same issue with my GitManagement module that uses It also fails when using the With the help of the PowerShell discord, I also tried to switch to dot sourcing the classes as I will try out your workaround next. Edit: Workaround works. 👍 |
Prerequisites
Steps to reproduce
Create two files in a directory.
test.ps1
testvalidate.psm1
Expected behavior
Actual behavior
Error details
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: