File size: 1,740 Bytes
35bdde1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
param(
[Parameter(Mandatory = $true)]
[string]$Path,
[Parameter(Mandatory = $false)]
[string]$SamplePath,
[switch]$Strict
)
$ErrorActionPreference = 'Stop'
function Fail([string]$Message) {
Write-Error $Message
exit 1
}
function Parse-Json([string]$JsonText, [string]$Label) {
try {
return ($JsonText | ConvertFrom-Json -ErrorAction Stop)
} catch {
Fail "Invalid JSON ($Label): $($_.Exception.Message)"
}
}
if (-not (Test-Path -LiteralPath $Path)) {
Fail "File not found: $Path"
}
$raw = Get-Content -LiteralPath $Path -Raw
$json = Parse-Json -JsonText $raw -Label $Path
if ($null -eq $json -or $json.GetType().Name -ne 'PSCustomObject') {
Fail "Expected a JSON object at top-level: $Path"
}
if ($Strict -and -not $SamplePath) {
Fail "Strict mode requires -SamplePath (exported from your Dify)."
}
if ($SamplePath) {
if (-not (Test-Path -LiteralPath $SamplePath)) {
Fail "Sample file not found: $SamplePath"
}
$sampleRaw = Get-Content -LiteralPath $SamplePath -Raw
$sample = Parse-Json -JsonText $sampleRaw -Label $SamplePath
if ($null -eq $sample -or $sample.GetType().Name -ne 'PSCustomObject') {
Fail "Expected a JSON object at top-level (sample): $SamplePath"
}
$sampleKeys = @($sample.PSObject.Properties.Name | Sort-Object)
$keys = @($json.PSObject.Properties.Name | Sort-Object)
$missing = @()
foreach ($k in $sampleKeys) {
if ($keys -notcontains $k) { $missing += $k }
}
if ($missing.Count -gt 0) {
$msg = "Missing top-level keys vs sample: " + ($missing -join ', ')
if ($Strict) { Fail $msg } else { Write-Warning $msg }
}
}
Write-Host "OK: $Path is valid JSON. Top-level keys: $(@($json.PSObject.Properties.Name) -join ', ')"
exit 0
|