Add parameter validation functions and enhance tests for configuration data
This commit is contained in:
@@ -42,6 +42,7 @@ function Assert-ConfigurationDataParameter {
|
||||
return
|
||||
}
|
||||
|
||||
Assert-ConfigurationDataParameterType -Name $Name -Definition $Definition -Value $Value
|
||||
Assert-ConfigurationDataParameterAllowedValue -Name $Name -Definition $Definition -Value $Value
|
||||
|
||||
if(Test-ConfigurationDataMapContainsKey -Map $Definition -Key "MinLength"){
|
||||
@@ -64,4 +65,20 @@ function Assert-ConfigurationDataParameter {
|
||||
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]."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,16 @@ function Assert-ConfigurationDataParameterAllowedValue {
|
||||
return
|
||||
}
|
||||
|
||||
if($Value -is [System.Array] -and $Value -isnot [string]){
|
||||
foreach($Item in @($Value)){
|
||||
if($AllowedValues -notcontains $Item){
|
||||
throw "Parameter [$Name] value [$Item] is not allowed. Allowed values are: $($AllowedValues -join ', ')."
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if($AllowedValues -notcontains $Value){
|
||||
throw "Parameter [$Name] value [$Value] is not allowed. Allowed values are: $($AllowedValues -join ', ')."
|
||||
}
|
||||
|
||||
21
Private/Assert-ConfigurationDataParameterNumericValue.ps1
Normal file
21
Private/Assert-ConfigurationDataParameterNumericValue.ps1
Normal file
@@ -0,0 +1,21 @@
|
||||
function Assert-ConfigurationDataParameterNumericValue {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Name,
|
||||
|
||||
[AllowNull()]
|
||||
$Value
|
||||
)
|
||||
|
||||
if($null -eq $Value -or $Value -is [bool] -or ($Value -isnot [ValueType])){
|
||||
throw "Parameter [$Name] value [$Value] is not numeric."
|
||||
}
|
||||
|
||||
try {
|
||||
[void]([decimal]$Value)
|
||||
} catch {
|
||||
throw "Parameter [$Name] value [$Value] is not numeric."
|
||||
}
|
||||
}
|
||||
61
Private/Assert-ConfigurationDataParameterType.ps1
Normal file
61
Private/Assert-ConfigurationDataParameterType.ps1
Normal file
@@ -0,0 +1,61 @@
|
||||
function Assert-ConfigurationDataParameterType {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]
|
||||
$Name,
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
$Definition,
|
||||
|
||||
[AllowNull()]
|
||||
$Value
|
||||
)
|
||||
|
||||
if(-not (Test-ConfigurationDataMapContainsKey -Map $Definition -Key "Type")){
|
||||
return
|
||||
}
|
||||
|
||||
$TypeName = ([string](Get-ConfigurationDataMapValue -Map $Definition -Key "Type")).ToLowerInvariant()
|
||||
|
||||
switch($TypeName){
|
||||
"string" {
|
||||
if($Value -isnot [string]){
|
||||
throw "Parameter [$Name] expects type [string], but received [$($Value.GetType().Name)]."
|
||||
}
|
||||
}
|
||||
{ $_ -in @("int", "integer") } {
|
||||
if(-not (Test-ConfigurationDataValueIsInteger -Value $Value)){
|
||||
throw "Parameter [$Name] expects type [int], but received [$($Value.GetType().Name)]."
|
||||
}
|
||||
}
|
||||
{ $_ -in @("bool", "boolean") } {
|
||||
if($Value -isnot [bool]){
|
||||
throw "Parameter [$Name] expects type [bool], but received [$($Value.GetType().Name)]."
|
||||
}
|
||||
}
|
||||
"array" {
|
||||
if($Value -isnot [System.Array] -or $Value -is [string]){
|
||||
throw "Parameter [$Name] expects type [array], but received [$($Value.GetType().Name)]."
|
||||
}
|
||||
}
|
||||
{ $_ -in @("hashtable", "object") } {
|
||||
if(-not (Test-ConfigurationDataMap -Value $Value)){
|
||||
throw "Parameter [$Name] expects type [hashtable], but received [$($Value.GetType().Name)]."
|
||||
}
|
||||
}
|
||||
"securestring" {
|
||||
if(($Value -isnot [string]) -and ($Value -isnot [System.Security.SecureString]) -and (-not (Test-ConfigurationDataMap -Value $Value))){
|
||||
throw "Parameter [$Name] expects type [secureString], but received [$($Value.GetType().Name)]."
|
||||
}
|
||||
}
|
||||
"credential" {
|
||||
if(($Value -isnot [System.Management.Automation.PSCredential]) -and (-not (Test-ConfigurationDataMap -Value $Value))){
|
||||
throw "Parameter [$Name] expects type [credential], but received [$($Value.GetType().Name)]."
|
||||
}
|
||||
}
|
||||
default {
|
||||
throw "Parameter [$Name] uses unsupported type [$TypeName]."
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Private/Test-ConfigurationDataValueIsInteger.ps1
Normal file
20
Private/Test-ConfigurationDataValueIsInteger.ps1
Normal file
@@ -0,0 +1,20 @@
|
||||
function Test-ConfigurationDataValueIsInteger {
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[AllowNull()]
|
||||
$Value
|
||||
)
|
||||
|
||||
if($null -eq $Value -or $Value -is [bool]){
|
||||
return $false
|
||||
}
|
||||
|
||||
return ($Value -is [byte]) -or
|
||||
($Value -is [sbyte]) -or
|
||||
($Value -is [int16]) -or
|
||||
($Value -is [uint16]) -or
|
||||
($Value -is [int]) -or
|
||||
($Value -is [uint32]) -or
|
||||
($Value -is [long]) -or
|
||||
($Value -is [uint64])
|
||||
}
|
||||
Reference in New Issue
Block a user