Files
Resolve-DSCConfigurationData/Private/Assert-ConfigurationDataParameter.ps1
Torsten Brendgen 4b83abc7f2 initial Commit
2026-06-27 00:36:04 +02:00

68 lines
2.5 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-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]."
}
}
}