question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validate interactive parameter values after they are entered

See original GitHub issue

Summary of the new feature/enhancement

Parameters used interactively should be validated immediately and allow/require the user to provide valid input. That is what the user must do anyway.

Proposed technical implementation details (optional)

It would be helpful if parameter validation would lead the interactive user to success sooner rather than later.

The following script requuires a two (2) valid paths. If the paths are entered interactively, the user does not know of a failed validation until after ALL parameters are entered.

PS C:\src\t> Get-Content -Path '.\parammessage.ps1'
[cmdletbinding()]
Param (
    [Parameter(Mandatory=$true, HelpMessage='Enter a valid path 1')]
    [ValidateScript({ Test-Path $_ })]
    [string]$Path1
    ,[Parameter(Mandatory=$true, HelpMessage='Enter a valid path 2')]
    [ValidateScript({ Test-Path $_ })]
    [string]$Path2
)

Write-Host "+++$Path1==="
Write-Host "+++$Path2==="
PS C:\src\t> ($PSVersionTable.PSVersion).ToString()
7.0.0-preview.3
PS C:\src\t> .\parammessage.ps1

cmdlet parammessage.ps1 at command pipeline position 1
Supply values for the following parameters:
(Type !? for Help.)
Path1: asdf
Path2: C:\
C:\src\t\parammessage.ps1 : Cannot validate argument on parameter 'Path1'. The " Test-Path $_ " validation script for the argument with value "asdf" did not return a result of True. Determine why the validation script failed, and then try the command again.
At line:1 char:1
+ .\parammessage.ps1
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidData: (:) [parammessage.ps1], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,parammessage.ps1

What I would like to see is the interactive prompt use the HelpMessage specified and give the user an opportunity to enter valid information if needed.

PS C:\src\t> .\parammessage.ps1

cmdlet parammessage.ps1 at command pipeline position 1
Enter a valid path 1
Path1: asdf
Entry is not valid.
Enter a valid path 1
Path1:

If the entry for Path1 is not valid, the script will fail. I do not see benefit in requiring ALL parameters to be entered before informing the user that one will not pass validation.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:16 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
Liturgistcommented, Sep 21, 2019

Many thanks to @KirkMunro and @iSazonov. I finally got to where I could build and have found my way down to .\src\Microsoft.PowerShell.ConsoleHost\host\msh\ConsoleHostUserInterfacePrompt.cs.

2reactions
KirkMunrocommented, Sep 17, 2019

So you mean when a command has mandatory parameters that are not provided by a user, resulting in individual prompts for the mandatory parameters, PowerShell should perform parameter validation on the values entered at the prompt when they are entered, and re-prompt the user for the parameter if they provided a value that is invalid, to guide users toward a path of successful invocation? That makes sense to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interactive parameter input
Enter a value for each parameter. Use empty quotes ("") to set a value to an empty string. Any blank entries are treated...
Read more >
Interactive parameter asks for value to be entered at once
I have the following Main class with subcommands: @Command(description = "MyTools for testing", name = "MyTools", mixinStandardHelpOptions ...
Read more >
Data Validation – How to Check User Input on HTML ...
What happens when invalid data is entered? This will help you define the user experience of the validation - whether to show an...
Read more >
Use parameters to ask for input when running a query
In the Query Parameters box, in the Parameter column, enter the prompt for each parameter you want to specify a data type for....
Read more >
Chapter 8 – Input Validation
Input validation code checks that values entered by the user, such as text from the input() function, are formatted correctly.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found