-
-
Save JamesNK/7e6d026c8b78c049bb1e1b6fb0ed85cf to your computer and use it in GitHub Desktop.
$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 = @' | |
{"prop1":"value"} | |
'@ | |
$SchemaJson = @' | |
{"type":"array"} | |
'@ | |
write-host "=========================" -foregroundcolor "white" | |
write-host "Json.NET Schema Validator" -foregroundcolor "white" | |
write-host "=========================" -foregroundcolor "white" | |
write-host | |
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" | |
} | |
write-host |
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
@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).
Unblocking the zip file solved initial problems for me as well, thanks ChristoperGrossert!
For some reason it does not work in vscode, however it does work in command prompt and ISE with no code changes.
You can do the same thing with the following lines:
$token = [Newtonsoft.Json.Linq.JToken]::Parse( $Json )
$schemaObj = [Newtonsoft.Json.Schema.JSchema]::Parse( $Schema )
$errorMessages = New-Object -TypeName System.Collections.Generic.List[string]
$result = [Newtonsoft.Json.Schema.SchemaExtensions]::IsValid( $token, $schemaObj, [ref] $errorMessages )
if ( $errorMessages.Count ) {
$result = $false
$errorMessages | Write-Output
}
Interrogate $result for your success or failure. I encapsulated this into a cmdlet to make it more portable.
After playing with it a bit, I got this to work in vscode:
Leaving the json and schema as strings resolved the type mismatch errors
$moduleRootPath = Split-Path -Path ( Split-Path -Path $PSCommandPath -Parent ) -Parent
Add-Type -Path "$moduleRootPath\bin\Newtonsoft.Json.dll"
Add-Type -Path "$moduleRootPath\bin\Newtonsoft.Json.Schema.dll"
$result = [Newtonsoft.Json.Schema.SchemaExtensions]::IsValid( $json, $schema, [ref] $errorMessages )
if ( -not $result -or $errorMessages.Count ) {
$result = $false
$errorMessages | Write-Error
}
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?
Thanks
--Vlad