Add parameter validation functions and enhance tests for configuration data

This commit is contained in:
Torsten Brendgen
2026-06-27 14:22:37 +02:00
parent 3e017fc926
commit 086020c3df
7 changed files with 371 additions and 28 deletions

121
Readme.md
View File

@@ -1,4 +1,4 @@
# Resolve-DSCConfigurationData
# Resolve-DSCConfigurationData
`Resolve-DSCConfigurationData` resolves parameter, variable, and expression references in DSC configuration data.
@@ -47,6 +47,10 @@ This block shows all currently supported parameter properties.
MinLength = 2
MaxLength = 32
# Optional numeric range validation. Applies to numeric values such as Type = 'int'.
MinValue = 1
MaxValue = 65535
# Optional regex validation.
Pattern = '^[A-Za-z][A-Za-z0-9_-]*$'
@@ -75,11 +79,72 @@ This block shows all currently supported parameter properties.
Notes:
- `Value` wins over `DefaultValue`.
- `Required`, `AllowedValues`, `MinLength`, `MaxLength`, and `Pattern` are validated by the resolver.
- `Type`, `Required`, `AllowedValues`, `MinLength`, `MaxLength`, `MinValue`, `MaxValue`, and `Pattern` are validated by the resolver.
- `AllowedValues` validates scalar values directly and array values item by item.
- `Sensitive` is currently metadata only.
- `Deprecated.Message` can be a string or a localized hashtable.
- Optional string parameters can allow empty values with a pattern like `'^$|^[A-Za-z][A-Za-z0-9_-]*$'`.
Supported parameter types:
- `string`
- `int` / `integer`
- `bool` / `boolean`
- `array`
- `hashtable` / `object`
- `secureString`
- `credential`
`secureString` and `credential` are intended for secret references, not raw secrets in PSD1 files:
```powershell
FarmPassphrase = @{
Type = 'secureString'
Required = $true
Sensitive = $true
Value = @{
SecretName = 'SharePointFarmPassphrase'
}
}
SetupCredential = @{
Type = 'credential'
Required = $true
Sensitive = $true
Value = @{
CredentialName = 'SharePointSetup'
}
}
```
Array values can be restricted item by item:
```powershell
ServerRoles = @{
Type = 'array'
Value = @(
'WebFrontEnd',
'Application'
)
AllowedValues = @(
'WebFrontEnd',
'Application',
'Search'
)
}
```
Numeric values can be restricted with `MinValue` and `MaxValue`:
```powershell
SqlPort = @{
Type = 'int'
DefaultValue = 1433
MinValue = 1
MaxValue = 65535
}
```
## Variable Example
Variables may reference parameters and other variables. Nested variable references are supported. Circular references are rejected.
@@ -93,7 +158,7 @@ Variables may reference parameters and other variables. Nested variable referenc
}
DomainLabel = @{
Type = 'string'
Value = 'LAN'
Value = 'corp'
}
Landscape = @{
Type = 'string'
@@ -122,9 +187,9 @@ Example output:
```text
StageCode : TST
DatabasePrefix : SharePoint_LAN_TST
ServiceDbPrefix : SharePoint_LAN_TST_Services
ConfigDbName : SharePoint_LAN_TST_Farm_Config
DatabasePrefix : SharePoint_corp_TST
ServiceDbPrefix : SharePoint_corp_TST_Services
ConfigDbName : SharePoint_corp_TST_Farm_Config
```
## Functions
@@ -149,12 +214,12 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
```powershell
"[format('{0}_{1}_{2}', parameters('DatabasePrefix'), parameters('DomainLabel'), parameters('Landscape'))]"
# SharePoint_LAN_Test
# SharePoint_corp_Test
```
```powershell
"[joinNotEmpty('_', parameters('DatabasePrefix'), parameters('DomainLabel'), '', 'Services')]"
# SharePoint_LAN_Services
# SharePoint_corp_Services
```
```powershell
@@ -185,13 +250,13 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
### Case And Text
```powershell
"[toLower('BGW-LAN')]"
# bgw-lan
"[toLower('Contoso-CORP')]"
# contoso-corp
```
```powershell
"[toUpper('bgw-lan')]"
# BGW-LAN
"[toUpper('contoso-corp')]"
# CONTOSO-CORP
```
```powershell
@@ -201,8 +266,8 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
```
```powershell
"[replace('BGW-LAN', '-', '_')]"
# BGW_LAN
"[replace('Contoso-CORP', '-', '_')]"
# Contoso_CORP
```
```powershell
@@ -211,7 +276,7 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
```
```powershell
"[indexOf('BGW', 1)]"
"[indexOf('CON', 1)]"
# G
```
@@ -228,23 +293,23 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
### Name Cleanup
```powershell
"[sanitizeName(' SharePoint LAN/Test DB ')]"
# SharePoint_LAN_Test_DB
"[sanitizeName(' SharePoint corp/Test DB ')]"
# SharePoint_corp_Test_DB
```
```powershell
"[sanitizeName(' SharePoint LAN/Test DB ', '-')]"
# SharePoint-LAN-Test-DB
"[sanitizeName(' SharePoint corp/Test DB ', '-')]"
# SharePoint-corp-Test-DB
```
```powershell
"[normalizeSeparator('__SharePoint___LAN_Test__', '_')]"
# SharePoint_LAN_Test
"[normalizeSeparator('__SharePoint___corp_Test__', '_')]"
# SharePoint_corp_Test
```
```powershell
"[prefixIfNotEmpty('LAN', 'BGW-')]"
# BGW-LAN
"[prefixIfNotEmpty('corp', 'Contoso-')]"
# Contoso-corp
```
```powershell
@@ -256,17 +321,17 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
```powershell
"[split(parameters('DomainFQDN'), '.')]"
# @('bgw-online', 'de')
# @('contoso', 'com')
```
```powershell
"[join(split(parameters('DomainFQDN'), '.'), '_')]"
# bgw-online_de
# contoso_com
```
```powershell
"[first(split(parameters('DomainFQDN'), '.'))]"
# bgw-online
# contoso
```
```powershell
@@ -276,7 +341,7 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
```powershell
"[take(split(parameters('DomainFQDN'), '.'), 1)]"
# @('bgw-online')
# @('contoso')
```
```powershell
@@ -307,7 +372,7 @@ ConfigDbName : SharePoint_LAN_TST_Farm_Config
```
```powershell
"[startsWith(parameters('DomainFQDN'), 'bgw')]"
"[startsWith(parameters('DomainFQDN'), 'contoso')]"
# True
```