Skip to content

Instantly share code, notes, and snippets.

@camous
Created May 30, 2019 20:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camous/d4e134d717fd786069b0f88a200736b6 to your computer and use it in GitHub Desktop.
Save camous/d4e134d717fd786069b0f88a200736b6 to your computer and use it in GitHub Desktop.
devops json schema powershell core validation
$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