Public/Test-PCOciSnapshot.ps1
|
function Test-PCOciSnapshot { <# .SYNOPSIS Validates a snapshot or usage file against its PowerCraft.OCI schema. .DESCRIPTION Checks the schema name and version, the required arrays and record fields, id ordering, reference integrity between backups, volumes, instances and policies, and the absence of any metadata-shaped property. Returns a result with the findings. A usage file is checked against the usage schema. When there are findings, an error is written as well, so a pipeline that runs with ErrorActionPreference Stop fails; use -Quiet to get the result object only. .PARAMETER Path The snapshot or usage file. .PARAMETER Quiet Do not write an error when there are findings. .EXAMPLE Test-PCOciSnapshot .\data\snapshot.json #> [CmdletBinding()] [OutputType([pscustomobject])] param( [Parameter(Mandatory, Position = 0, ValueFromPipeline, ValueFromPipelineByPropertyName)] [Alias('SnapshotPath', 'UsagePath', 'FullName')] [string]$Path, [switch]$Quiet ) process { $resolved = if (Test-Path -LiteralPath $Path) { (Resolve-Path -LiteralPath $Path).Path } else { $Path } $result = [PowerCraft.OCI.Core.Usage.FileValidator]::ValidateFile($resolved) $output = [pscustomobject]@{ Path = $resolved IsValid = $result.IsValid Findings = @($result.Findings) } if (-not $result.IsValid -and -not $Quiet) { Write-Error -Message ("Snapshot '{0}' has {1} finding(s): {2}" -f $resolved, $result.Findings.Count, ($result.Findings -join '; ')) -ErrorId 'PowerCraft.OCI.SnapshotInvalid' -Category InvalidData -TargetObject $output } $output } } |