Skip to content

🩹 [Patch]: Add Test-Toml to validate TOML without throwing - #22

Merged
Marius Storhaug (MariusStorhaug) merged 3 commits into
build-toml-modulefrom
feat-test-toml
Jul 26, 2026
Merged

🩹 [Patch]: Add Test-Toml to validate TOML without throwing#22
Marius Storhaug (MariusStorhaug) merged 3 commits into
build-toml-modulefrom
feat-test-toml

Conversation

@MariusStorhaug

Copy link
Copy Markdown
Member

Adds Test-Toml, a pure-validator that returns [bool] — never throws — so scripts can safely probe TOML content before (or instead of) parsing it. The design mirrors PowerShell's built-in Test-Json.

New: Validate TOML content safely

Test-Toml wraps ConvertFrom-Toml in a try/catch. On success it returns $true; on any parser error it writes a non-terminating error to the error stream and returns $false.

# Validate a string
Test-Toml -InputObject 'key = "value"'    # $true
'[invalid' | Test-Toml                    # $false + error written

# Validate a file
Test-Toml -Path .\Cargo.toml              # $true if file is valid TOML
Test-Toml -LiteralPath 'C:\app\cfg.toml'  # literal path variant

Parameter sets

Parameter Set Notes
-InputObject Default Pipeline-capable; [AllowEmptyString()] so '' returns $false
-Path Path Resolves relative paths
-LiteralPath LiteralPath No wildcard expansion

Key implementation notes

  • [AllowEmptyString()] is required on InputObject: PowerShell's mandatory parameter binding rejects '' without it, whereas the correct behaviour is $false.
  • Write-Error is always called from inside a catch block with -ErrorAction Continue to ensure non-terminating behaviour even when $ErrorActionPreference = 'Stop' is set module-wide.
  • No new parser logic — reuses ConvertFrom-Toml to keep validation consistent with parsing.

Technical Details

  • File added: src/functions/public/Test-Toml.ps1
  • Tests added: Describe 'Test-Toml' block in tests/Toml.Tests.ps1 (11 new tests; total 135, all passing)
  • PSScriptAnalyzer: No new warnings introduced (pre-existing BOM and OutputType notices in other files unchanged)
  • No alias: The issue mentions Test-Tml but aliases are not in the current module standards
Details

Closes #4

Implement Test-Toml that validates TOML content without throwing.
Mirrors PowerShell's Test-Json pattern:
- Returns [bool] -- $true for valid, $false for invalid
- Writes non-terminating errors via Write-Error on failure
- Supports InputObject (pipeline), Path, and LiteralPath parameter sets
- Uses [AllowEmptyString()] so empty string returns $false gracefully

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add Describe 'Test-Toml' block covering:
- Module registration and [OutputType([bool])] declaration
- Valid TOML string, [bool] return type, pipeline input
- Invalid TOML: returns $false and non-terminating error
- Empty string: returns $false
- Duplicate-key document: returns $false
- -Path: valid file returns $true, invalid returns $false,
  non-existent file returns $false and error
- -LiteralPath: valid file returns $true and [bool] type

Also update module-level 'exposes the expected public commands'
test to include 'Test-Toml'.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant