Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Validate Schema from PowerShell
$ErrorActionPreference = "Stop"
$NewtonsoftJsonPath = Resolve-Path -Path "bin\Newtonsoft.Json.dll"
$NewtonsoftJsonSchemaPath = Resolve-Path -Path "bin\Newtonsoft.Json.Schema.dll"
Add-Type -Path $NewtonsoftJsonPath
Add-Type -Path $NewtonsoftJsonSchemaPath
$source = @'
public class Validator
public static System.Collections.Generic.IList<string> Validate(Newtonsoft.Json.Linq.JToken token, Newtonsoft.Json.Schema.JSchema schema)
System.Collections.Generic.IList<string> messages;
Newtonsoft.Json.Schema.SchemaExtensions.IsValid(token, schema, out messages);
return messages;
Add-Type -TypeDefinition $source -ReferencedAssemblies $NewtonsoftJsonPath,$NewtonsoftJsonSchemaPath
$Json = @'
$SchemaJson = @'
write-host "=========================" -foregroundcolor "white"
write-host "Json.NET Schema Validator" -foregroundcolor "white"
write-host "=========================" -foregroundcolor "white"
write-host "Json: $Json"
write-host "SchemaJson: $SchemaJson"
$Token = [Newtonsoft.Json.Linq.JToken]::Parse($Json)
$Schema = [Newtonsoft.Json.Schema.JSchema]::Parse($SchemaJson)
$ErrorMessages = [Validator]::Validate($Token, $Schema)
$IsValid = $ErrorMessages.Count -eq 0
write-host "Schema is valid: $IsValid" -foregroundcolor "white"
foreach ($ErrorMessage in $ErrorMessages) {
write-host $ErrorMessage -foregroundcolor "red"

This comment has been minimized.

Copy link

@vladpopov vladpopov commented Mar 20, 2018

Hi James,
this code sample is working fine using Powershell.exe or Powershell ISE but not on Visual Studio Code. I wrote more details on Stackoverflow question. Can you please have a quick look to it?



This comment has been minimized.

Copy link

@Mark-Foresta Mark-Foresta commented Nov 15, 2018

I have been unable to get this to work. When running this code I get the error:

Add-Type : Could not load file or assembly PATH\Newtonsoft.Json.Schema.dll' or one of 
its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

I have verified that all files are unblocked


This comment has been minimized.

Copy link

@ChristopherGrossert ChristopherGrossert commented Oct 1, 2019

@Mark-Foresta For me, the issue was resolved by unblocking the Newtonsoft.Json.Schema zip file itself before unzipping the files (instead of unblocking the dll files).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment