Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PackageVersion Include="HtmlAgilityPack" Version="1.12.4" />
<PackageVersion Include="ICSharpCode.Decompiler" Version="9.1.0.7988" />
<PackageVersion Include="Jint" Version="4.5.0" />
<PackageVersion Include="JsonSchema.Net" Version="7.4.0" />
<PackageVersion Include="JsonSchema.Net" Version="[8.0.5]" />
<PackageVersion Include="Markdig" Version="0.44.0" />
<PackageVersion Include="Microsoft.Playwright" Version="1.57.0" />
<PackageVersion Include="Newtonsoft.Json" Version="13.0.4" />
Expand Down
39 changes: 31 additions & 8 deletions src/Docfx.Build.SchemaDriven/Validators/SchemaValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,63 @@
using Docfx.Common;
using Json.Schema;

#nullable enable

namespace Docfx.Build.SchemaDriven;

public class SchemaValidator
{
private readonly JsonSchema _schema;

private static readonly EvaluationOptions DefaultOptions = new()
private static readonly EvaluationOptions DefaultEvaluationOptions = new()
{
ValidateAgainstMetaSchema = false,
OutputFormat = OutputFormat.List,
};

private static readonly JsonDocumentOptions DefaultJsonDocumentOptions = new()
{
AllowTrailingCommas = true,
};

static SchemaValidator()
{
SchemaRegistry.Global.Register(new("http://dotnet.github.io/docfx/schemas/v1.0/schema.json#"), MetaSchemas.Draft7);
SchemaRegistry.Global.Register(new("https://dotnet.github.io/docfx/schemas/v1.0/schema.json#"), MetaSchemas.Draft7);
Uri[] uris =
[
new Uri("http://dotnet.github.io/docfx/schemas/v1.0/schema.json#"),
new Uri("https://dotnet.github.io/docfx/schemas/v1.0/schema.json#"),
];

foreach (var uri in uris)
{
SchemaRegistry.Global.Register(uri, MetaSchemas.Draft7);
DialectRegistry.Global.Register(Dialect.Draft07.With([], id: uri));
}
}

public SchemaValidator(string json)
{
_schema = JsonSchema.FromText(json, new() { AllowTrailingCommas = true });
var builldOptions = new BuildOptions()
{
// Create SchemaRegistry instance to avoid exception.
// https://github.com/json-everything/json-everything/issues/957
SchemaRegistry = new SchemaRegistry(),
Dialect = Dialect.Draft07,
};

_schema = JsonSchema.FromText(json, builldOptions, jsonOptions: DefaultJsonDocumentOptions);
}

public void Validate(object obj)
{
var json = JsonSerializer.Serialize(obj);
var result = _schema.Evaluate(JsonDocument.Parse(json), DefaultOptions);
var result = _schema.Evaluate(JsonDocument.Parse(json, DefaultJsonDocumentOptions).RootElement, DefaultEvaluationOptions);

if (result.IsValid)
return;

foreach (var detail in result.Details)
foreach (var detail in result.Details ?? [])
{
if (detail.HasErrors)
if (detail.Errors != null)
{
foreach (var (type, message) in detail.Errors)
{
Expand Down
13 changes: 1 addition & 12 deletions test/docfx.Tests/Utilities/JsonSchemaUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ namespace Docfx.Tests;

internal static class JsonSchemaUtility
{
public static readonly JsonSerializerOptions DefaultSerializerOptions = new()
{
PropertyNameCaseInsensitive = true,
AllowTrailingCommas = true,
ReadCommentHandling = JsonCommentHandling.Skip,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
};

public static readonly EvaluationOptions DefaultEvaluationOptions = new()
{
ValidateAgainstMetaSchema = false,
OutputFormat = OutputFormat.List,
};

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

var schema = JsonSchema.FromFile(jsonSchemaPath, DefaultSerializerOptions);
var schema = JsonSchema.FromFile(jsonSchemaPath, new Json.Schema.BuildOptions { SchemaRegistry = new SchemaRegistry() });

var result = schema.Evaluate(jsonElement, DefaultEvaluationOptions);
return result;
Expand Down