Scripts that load PSD1 content from external sources (config registries, Git blobs, HTTP) need to validate it before parsing. The built-in Import-PowerShellDataFile only reads files and throws on invalid input — there is no string-based or boolean-returning equivalent.
PowerShell's Test-Json sets the established pattern for serialization-format validators: returns [bool], writes parser errors to the error stream when invalid.
Request
Desired capability
Test-PowerShellDataFile -Path module.psd1 # validate a file
$content | Test-PowerShellDataFile # validate a string
'@{ broken' | Test-PowerShellDataFile # returns $false, writes error
Parameters
| Parameter |
Description |
-Path |
File path to validate (parameter set: Path) |
-LiteralPath |
Literal path (parameter set: LiteralPath) |
-InputObject |
String to validate (ValueFromPipeline, default set) |
-AllowedTypes |
Optional list of allowed value types beyond the PSD1 default subset |
Validation rules
- Content parses as a single hashtable expression at the top level
- Only PSD1-allowed types appear (hashtables, arrays, strings, numbers, booleans,
null, [datetime], [guid], [char], [scriptblock])
- No command invocations, variable references, or other dynamic constructs (the same safety check
Import-PowerShellDataFile performs)
Acceptance criteria
- Returns
$true when content is a valid PSD1 expression
- Returns
$false when content is invalid; the parser error is written to the error stream
- Accepts pipeline input as a string
- Mirrors
Test-Json parameter shape and behavior
- Equivalent to attempting
Import-PowerShellDataFile on a temp file but without the file I/O
Technical decisions
Implementation: Wraps ConvertFrom-PowerShellDataFile (tracked separately) in try/catch. On exception, writes the parser error via Write-Error and returns $false. On success, returns $true.
Function placement: src/functions/public/Test-PowerShellDataFile.ps1.
No new parser logic: Reuses the AST-based parser from ConvertFrom-PowerShellDataFile. This keeps validation behavior consistent with parsing behavior and matches the safety guarantees of the built-in Import-PowerShellDataFile.
Output type: [bool] only.
Alias: Test-Psd1.
Implementation plan
Related
- ConvertFrom/ConvertTo-PowerShellDataFile (prerequisite — separate issue)
Scripts that load PSD1 content from external sources (config registries, Git blobs, HTTP) need to validate it before parsing. The built-in
Import-PowerShellDataFileonly reads files and throws on invalid input — there is no string-based or boolean-returning equivalent.PowerShell's
Test-Jsonsets the established pattern for serialization-format validators: returns[bool], writes parser errors to the error stream when invalid.Request
Desired capability
Parameters
-PathPath)-LiteralPathLiteralPath)-InputObjectValueFromPipeline, default set)-AllowedTypesValidation rules
null,[datetime],[guid],[char],[scriptblock])Import-PowerShellDataFileperforms)Acceptance criteria
$truewhen content is a valid PSD1 expression$falsewhen content is invalid; the parser error is written to the error streamTest-Jsonparameter shape and behaviorImport-PowerShellDataFileon a temp file but without the file I/OTechnical decisions
Implementation: Wraps
ConvertFrom-PowerShellDataFile(tracked separately) intry/catch. On exception, writes the parser error viaWrite-Errorand returns$false. On success, returns$true.Function placement:
src/functions/public/Test-PowerShellDataFile.ps1.No new parser logic: Reuses the AST-based parser from
ConvertFrom-PowerShellDataFile. This keeps validation behavior consistent with parsing behavior and matches the safety guarantees of the built-inImport-PowerShellDataFile.Output type:
[bool]only.Alias:
Test-Psd1.Implementation plan
Test-PowerShellDataFileinsrc/functions/public/Test-PowerShellDataFile.ps1withTest-Psd1aliasRelated