Files
Resolve-DSCConfigurationData/Private/Assert-ConfigurationDataParameter.ps1

85 lines
3.4 KiB
PowerShell

function Assert-ConfigurationDataParameter {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]
$Name,
[Parameter(Mandatory=$true)]
$Definition,
[AllowNull()]
$Value,
[Parameter(Mandatory=$true)]
[bool]
$HasValue
)
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "Deprecated"){
$Deprecated = Get-ConfigurationDataMapValue -Map $Definition -Key "Deprecated"
$Message = "Parameter [$Name] is deprecated."
if((Test-ConfigurationDataMap -Value $Deprecated) -and (Test-ConfigurationDataMapContainsKey -Map $Deprecated -Key "Message")){
$Message = Resolve-ConfigurationDataLocalizedText -Value (Get-ConfigurationDataMapValue -Map $Deprecated -Key "Message") -DefaultValue $Message
}elseif($Deprecated -is [string] -and -not [string]::IsNullOrWhiteSpace($Deprecated)){
$Message = $Deprecated
}
Write-Warning $Message
}
$Required = $false
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "Required"){
$Required = [bool](Get-ConfigurationDataMapValue -Map $Definition -Key "Required")
}
if($Required -and ((-not $HasValue) -or (Test-ConfigurationDataValueIsEmpty -Value $Value))){
throw "Parameter [$Name] is required."
}
if(-not $HasValue -or $null -eq $Value){
return
}
Assert-ConfigurationDataParameterType -Name $Name -Definition $Definition -Value $Value
Assert-ConfigurationDataParameterAllowedValue -Name $Name -Definition $Definition -Value $Value
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "MinLength"){
$MinLength = [int](Get-ConfigurationDataMapValue -Map $Definition -Key "MinLength")
if(([string]$Value).Length -lt $MinLength){
throw "Parameter [$Name] value length [$(([string]$Value).Length)] is less than MinLength [$MinLength]."
}
}
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "MaxLength"){
$MaxLength = [int](Get-ConfigurationDataMapValue -Map $Definition -Key "MaxLength")
if(([string]$Value).Length -gt $MaxLength){
throw "Parameter [$Name] value length [$(([string]$Value).Length)] is greater than MaxLength [$MaxLength]."
}
}
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "Pattern"){
$Pattern = [string](Get-ConfigurationDataMapValue -Map $Definition -Key "Pattern")
if(([string]$Value) -notmatch $Pattern){
throw "Parameter [$Name] value [$Value] does not match pattern [$Pattern]."
}
}
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "MinValue"){
Assert-ConfigurationDataParameterNumericValue -Name $Name -Value $Value
$MinValue = [decimal](Get-ConfigurationDataMapValue -Map $Definition -Key "MinValue")
if(([decimal]$Value) -lt $MinValue){
throw "Parameter [$Name] value [$Value] is less than MinValue [$MinValue]."
}
}
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "MaxValue"){
Assert-ConfigurationDataParameterNumericValue -Name $Name -Value $Value
$MaxValue = [decimal](Get-ConfigurationDataMapValue -Map $Definition -Key "MaxValue")
if(([decimal]$Value) -gt $MaxValue){
throw "Parameter [$Name] value [$Value] is greater than MaxValue [$MaxValue]."
}
}
}