Fall back to JsonSchema4 if JsonSchema is unavailable

For some reason, it looks like the NJsonSchema library exports a
NJsonSchema object if queried from .NET Core, but if queried
from .NET Framework (Windows PowerShell) it exports a
NJsonSchema4 object. From the same DLL.

This is a workaround to fallback to NJsonSchema4 if NJsonSchema
doesn't seem available, which makes the library work for
Windows PowerShell.
This commit is contained in:
Andrea Spadaccini 2020-06-11 18:12:05 +01:00 коммит произвёл Andrea Spadaccini
Родитель c4567e4946
Коммит 203fa19e22
1 изменённых файлов: 21 добавлений и 6 удалений

Просмотреть файл

@ -44,17 +44,32 @@ try {
$script:schema = $null
try {
Write-Verbose "Loading JSON schema..."
Write-Verbose "Reading JSON schema..."
$script:schemaPath = Get-Content $PSScriptRoot\featureflags.schema.json
$script:schema = [NJsonSchema.JSonSchema]::FromJsonAsync($script:schemaPath).GetAwaiter().GetResult()
Write-Verbose "Loaded JSON schema from featureflags.schema.json."
Write-Verbose $script:schema
} catch {
Write-Error "Error loading JSON schema"
Write-Host $_.Exception.Message
Write-Error "Error reading JSON schema: $($_.Exception.Message)"
throw
}
try {
Write-Verbose "Loading JSON schema..."
$script:schema = [NJsonSchema.JSonSchema]::FromJsonAsync($script:schemaPath).GetAwaiter().GetResult()
} catch {
$firstException = $_.Exception
# As a fallback, try reading using the JsonSchema4 object. The JSON schema library
# exposes that object to .NET Framework instead of JsonSchema for some reason.
try {
Write-Verbose "Loading JSON schema (fallback)..."
$script:schema = [NJsonSchema.JSonSchema4]::FromJsonAsync($script:schemaPath).GetAwaiter().GetResult()
} catch {
Write-Error "Error loading JSON schema: $($_.Exception.Message). First error: $($firstException.Message)."
Write-Host $_.Exception.Message
throw
}
}
Write-Verbose "Loaded JSON schema from featureflags.schema.json."
Write-Verbose $script:schema
<#
.SYNOPSIS
Validates feature flag configuration.