Private/Test-ValidInput.ps1
function Test-ValidInput { param( [object[]]$Prompts, [hashtable]$Inputs ) [bool]$valid = $true [string]$err_msg = $null foreach ($p in $Prompts) { if ($p.Mandatory -and -not $Inputs[$p.Name]) { if ($p.ValidateSet) { $err_msg = 'Make a selection.' } elseif ($p.InputType -eq [AnyBox.InputType]::Link) { $err_msg = 'Click the link.' } elseif ($p.Message) { $err_msg = "Missing required input for '{0}'" -f $p.Message.TrimEnd(':').Trim() } else { $err_msg = 'Missing required input.' } } elseif ($p.ValidateScript -and -not ($Inputs[$p.Name] | ForEach-Object -Process $p.ValidateScript)) { $err_msg = 'Invalid input' if ($p.Message) { $err_msg += " for '$($p.Message.TrimEnd(':'))'" } } elseif ($Inputs[$p.Name]) { [string]$failed_rule = $null if ($p.ValidateGreaterThan -or $p.ValidateLessThan) { $p.ValidateNumber = $true } if ($p.ValidateInteger -and $Inputs[$p.Name] -notmatch '^[0-9]+$') { $failed_rule = 'ValidateInteger' } elseif ($p.ValidateNumber -and $Inputs[$p.Name] -notmatch '^[+-]?([0-9]*[.])?[0-9]+$') { $failed_rule = 'ValidateNumber' } elseif ($p.ValidateGreaterThan -and -not $(([double]$Inputs[$p.Name]) -gt $p.ValidateGreaterThan)) { $failed_rule = "ValidateGreaterThan($($p.ValidateGreaterThan))" } elseif ($p.ValidateLessThan -and -not $(([double]$Inputs[$p.Name]) -lt $p.ValidateLessThan)) { $failed_rule = "ValidateLessThan($($p.ValidateLessThan))" } elseif ($p.ValidateAlphabetic -and $Inputs[$p.Name] -notmatch '^[a-zA-Z]+$') { $failed_rule = 'ValidateAlphabetic' } elseif ($p.ValidateAlphaNum -and $Inputs[$p.Name] -notmatch '^[a-zA-Z0-9]+$') { $failed_rule = 'ValidateAlphaNum' } elseif ($p.ValidateRegEx -and $Inputs[$p.Name] -notmatch $p.ValidateRegEx) { $failed_rule = "ValidateRegEx( $($p.ValidateRegEx) )" } if ($failed_rule) { $err_msg = 'Invalid input' if ($p.Message) { $err_msg += " for '$($p.Message.TrimEnd(':'))'" } $err_msg += [System.Environment]::NewLine + "Rule name: $failed_rule" } } if ($err_msg) { $valid = $false break } } return([PSCustomObject]@{ IsValid = $valid Message = $err_msg }) } |