Skip to content

Commit 626e904

Browse files
committed
deps: update JsonSchema.Net dependency to 8.x
1 parent 8b43a7a commit 626e904

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<PackageVersion Include="HtmlAgilityPack" Version="1.12.4" />
88
<PackageVersion Include="ICSharpCode.Decompiler" Version="9.1.0.7988" />
99
<PackageVersion Include="Jint" Version="4.5.0" />
10-
<PackageVersion Include="JsonSchema.Net" Version="7.4.0" />
10+
<PackageVersion Include="JsonSchema.Net" Version="8.0.5" />
1111
<PackageVersion Include="Markdig" Version="0.44.0" />
1212
<PackageVersion Include="Microsoft.Playwright" Version="1.57.0" />
1313
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />

src/Docfx.Build.SchemaDriven/Validators/SchemaValidator.cs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,40 +5,63 @@
55
using Docfx.Common;
66
using Json.Schema;
77

8+
#nullable enable
9+
810
namespace Docfx.Build.SchemaDriven;
911

1012
public class SchemaValidator
1113
{
1214
private readonly JsonSchema _schema;
1315

14-
private static readonly EvaluationOptions DefaultOptions = new()
16+
private static readonly EvaluationOptions DefaultEvaluationOptions = new()
1517
{
16-
ValidateAgainstMetaSchema = false,
1718
OutputFormat = OutputFormat.List,
1819
};
1920

21+
private static readonly JsonDocumentOptions DefaultJsonDocumentOptions = new()
22+
{
23+
AllowTrailingCommas = true,
24+
};
25+
2026
static SchemaValidator()
2127
{
22-
SchemaRegistry.Global.Register(new("http://dotnet.github.io/docfx/schemas/v1.0/schema.json#"), MetaSchemas.Draft7);
23-
SchemaRegistry.Global.Register(new("https://dotnet.github.io/docfx/schemas/v1.0/schema.json#"), MetaSchemas.Draft7);
28+
Uri[] uris =
29+
[
30+
new Uri("http://dotnet.github.io/docfx/schemas/v1.0/schema.json#"),
31+
new Uri("https://dotnet.github.io/docfx/schemas/v1.0/schema.json#"),
32+
];
33+
34+
foreach (var uri in uris)
35+
{
36+
SchemaRegistry.Global.Register(uri, MetaSchemas.Draft7);
37+
DialectRegistry.Global.Register(Dialect.Draft07.With([], id: uri));
38+
}
2439
}
2540

2641
public SchemaValidator(string json)
2742
{
28-
_schema = JsonSchema.FromText(json, new() { AllowTrailingCommas = true });
43+
var builldOptions = new BuildOptions()
44+
{
45+
// Create SchemaRegistry instance to avoid exception.
46+
// https://github.com/json-everything/json-everything/issues/957
47+
SchemaRegistry = new SchemaRegistry(),
48+
Dialect = Dialect.Draft07,
49+
};
50+
51+
_schema = JsonSchema.FromText(json, builldOptions, jsonOptions: DefaultJsonDocumentOptions);
2952
}
3053

3154
public void Validate(object obj)
3255
{
3356
var json = JsonSerializer.Serialize(obj);
34-
var result = _schema.Evaluate(JsonDocument.Parse(json), DefaultOptions);
57+
var result = _schema.Evaluate(JsonDocument.Parse(json, DefaultJsonDocumentOptions).RootElement, DefaultEvaluationOptions);
3558

3659
if (result.IsValid)
3760
return;
3861

39-
foreach (var detail in result.Details)
62+
foreach (var detail in result.Details ?? [])
4063
{
41-
if (detail.HasErrors)
64+
if (detail.Errors != null)
4265
{
4366
foreach (var (type, message) in detail.Errors)
4467
{

test/docfx.Tests/Utilities/JsonSchemaUtility.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,8 @@ namespace Docfx.Tests;
88

99
internal static class JsonSchemaUtility
1010
{
11-
public static readonly JsonSerializerOptions DefaultSerializerOptions = new()
12-
{
13-
PropertyNameCaseInsensitive = true,
14-
AllowTrailingCommas = true,
15-
ReadCommentHandling = JsonCommentHandling.Skip,
16-
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
17-
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
18-
WriteIndented = true,
19-
};
20-
2111
public static readonly EvaluationOptions DefaultEvaluationOptions = new()
2212
{
23-
ValidateAgainstMetaSchema = false,
2413
OutputFormat = OutputFormat.List,
2514
};
2615

@@ -38,7 +27,7 @@ public static EvaluationResults ValidateJsonSchema(JsonElement jsonElement, stri
3827
if (!File.Exists(jsonSchemaPath))
3928
throw new FileNotFoundException(jsonSchemaPath);
4029

41-
var schema = JsonSchema.FromFile(jsonSchemaPath, DefaultSerializerOptions);
30+
var schema = JsonSchema.FromFile(jsonSchemaPath, new Json.Schema.BuildOptions { SchemaRegistry = new SchemaRegistry() });
4231

4332
var result = schema.Evaluate(jsonElement, DefaultEvaluationOptions);
4433
return result;

0 commit comments

Comments
 (0)