Private/Test-ValidInput.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
function Test-ValidInput
{
    param(
        [object[]]$Prompts,
        [hashtable]$Inputs
    )

    [bool]$valid = $true
    [string]$err_msg = $null

    foreach ($prmpt in $Prompts)
    {
        if ($prmpt.ValidateNotEmpty -and -not $Inputs[$prmpt.Name]) {
                if ($prmpt.ValidateSet) {
                    $err_msg = 'Please make a selection.'
                }
                elseif ($prmpt.InputType -eq [AnyBox.InputType]::Link) {
                    $err_msg = 'Please click the link.'
                }
                elseif ($prmpt.Message) {
                    $err_msg = "Please provide input for '{0}'" -f $prmpt.Message.TrimEnd(':').Trim()
                }
                else {
                    $err_msg = 'Please provide input for required fields.'
                }
        }
        elseif ($prmpt.ValidateScript -and -not ($Inputs[$prmpt.Name] | ForEach-Object -Process $prmpt.ValidateScript)) {
            if ($prmpt.Message) {
                $err_msg = "Invalid input for '{0}'" -f $prmpt.Message.TrimEnd(':')
            }
            else {
                $err_msg = "Invalid input provided."
            }
        }

        if ($err_msg) {
            $valid = $false
            break
        }
    }

    return([PSCustomObject]@{
        Is_Valid = $valid
        Message = $err_msg
    })
}