Created
May 30, 2019 20:42
-
-
Save camous/d4e134d717fd786069b0f88a200736b6 to your computer and use it in GitHub Desktop.
devops json schema powershell core validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$schema = Get-Content $Env:schema | Out-String | |
$err = ""; | |
Get-ChildItem -Filter "*.json" | Where-Object { ($Env:exclude_json -split ',') -notcontains $_.Name} | ForEach-Object { | |
# first test only json parsing to not mixup error | |
$json = Get-Content $_ | Out-String -ErrorAction:SilentlyContinue | |
if(($json | Test-Json -ErrorAction:SilentlyContinue) -eq $true){ | |
# if json parsing is fine, let's check schema | |
if(($json | Test-Json -Schema $schema -ErrorAction:SilentlyContinue) -eq $true){ | |
Write-Host ($_.Name + "`t`tPassed") | |
} else { | |
$err += $_.Name + "`t`t" + $Error[0].ErrorDetails + "`r`n"; | |
} | |
} else { | |
$err += $_.Name + "`t`t" + $Error[0].Exception.Message + "`r`n"; | |
} | |
} | |
# only drop one error preventing | |
if($err -ne ""){ | |
Write-Error $err | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment