Skip to content

Add Test-Lua function for validation #8

@MariusStorhaug

Description

Scripts that load Lua content from external sources need to validate it before parsing. Currently the only signal is a thrown exception from ConvertFrom-Lua. 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-Lua -Path settings.lua          # validate a file
$content | Test-Lua                   # validate a string
'{ invalid' | Test-Lua                # 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)

Acceptance criteria

  • Returns $true when content is valid Lua table syntax
  • 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

Technical decisions

Implementation: Wraps ConvertFrom-Lua 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-Lua.ps1.

No new parser logic: Reuses the existing ConvertFrom-Lua parser. This keeps validation behavior consistent with parsing behavior.

Output type: [bool] only — never throws on invalid input (terminating errors only on argument errors).


Implementation plan

  • Add Test-Lua in src/functions/public/Test-Lua.ps1
  • Add Pester tests — valid string, invalid string, valid file, invalid file, pipeline input, error-stream contents on failure
  • Update README with usage example

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions