internal/functions/configuration/Register-DbatoolsConfigValidation.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
function Register-DbatoolsConfigValidation {
    <#
        .SYNOPSIS
            Registers a validation scriptblock for use with the configuration system.
 
        .DESCRIPTION
            Registers a validation scriptblock for use with the configuration system.
 
            The scriptblock must be designed according to a few guidelines:
            - It must not throw exceptions
            - It must accept a single parameter (the value to be tested)
            - It must return an object with three properties: 'Message', 'Value' and 'Success'.
            The Success property should be boolean and indicate whether the value is valid.
            The Value property contains the validated input. The scriptblock may legally convert the input (For example from string to int in case of integer validation)
            The message contains a string that will be passed along to an exception in case the input is NOT valid.
 
        .PARAMETER Name
            The name under which to register the validation scriptblock
 
        .PARAMETER ScriptBlock
            The scriptblock to register
 
        .EXAMPLE
            PS C:\> Register-DbatoolsConfigValidation -Name IntPositive -ScriptBlock $scriptblock
 
            Registers the scriptblock stored in $scriptblock as validation with the name IntPositive
       #>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]
        $Name,

        [Parameter(Mandatory)]
        [ScriptBlock]
        $ScriptBlock
    )

    [Sqlcollaborative.Dbatools.Configuration.ConfigurationHost]::Validation[$Name.ToLower()] = $ScriptBlock
}