Private/Test-DFToolSchema.ps1
|
#Requires -Version 7.0 function Test-DFToolSchema { <# .SYNOPSIS Validates a tool PSCustomObject against the DotForge tool schema. Returns $true if valid; populates -Errors with any violation messages. .DESCRIPTION Private validator for tool JSON records. Checks required fields and valid enum values. Errors are collected into a list and returned via the -Errors reference parameter. .PARAMETER Tool The tool PSCustomObject to validate (typically parsed from JSON). .PARAMETER Errors Reference to an array that will be populated with validation error messages. If validation passes, this array will be empty. .OUTPUTS [bool] - $true if valid, $false if any violations found. #> [CmdletBinding()] [OutputType([bool])] param( [Parameter(Mandatory)][PSCustomObject]$Tool, [ref]$Errors ) $errs = [System.Collections.Generic.List[string]]::new() # Helper: safely read a property from a PSCustomObject without throwing under StrictMode function PSProp ([PSCustomObject]$obj, [string]$key) { if ($null -eq $obj) { return $null } $p = $obj.PSObject.Properties[$key] if ($p) { return $p.Value } else { return $null } } # Required fields if (-not (PSProp $Tool 'name')) { $errs.Add("Missing required field: name") } if (-not (PSProp $Tool 'executable')) { $errs.Add("Missing required field: executable") } # type valid values $validToolTypes = @('exe', 'module') $toolType = PSProp $Tool 'type' if ($toolType -and $toolType -notin $validToolTypes) { $errs.Add("Invalid type '$toolType'. Valid: $($validToolTypes -join ', ')") } # xdg.method valid values $validMethods = @('default', 'env', 'config', 'wrapper', 'manual') $xdgMethod = PSProp (PSProp $Tool 'xdg') 'method' if ($xdgMethod -and $xdgMethod -notin $validMethods) { $errs.Add("Invalid xdg.method '$xdgMethod'. Valid: $($validMethods -join ', ')") } if ($Errors) { $Errors.Value = $errs.ToArray() } return $errs.Count -eq 0 } |