Skip to content

Add Test-PowerShellDataFile function for validation #14

Description

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

  • Add Test-PowerShellDataFile in src/functions/public/Test-PowerShellDataFile.ps1 with Test-Psd1 alias
  • Add Pester tests — valid string, invalid string (syntax error), disallowed type (e.g., command invocation), valid file, invalid file, pipeline input, error-stream contents
  • Update README with usage example

Related

  • ConvertFrom/ConvertTo-PowerShellDataFile (prerequisite — separate issue)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions