验证失败但缺少错误消息

Validation fails but error messages missing

我正在尝试使用以下代码根据特定模式验证 JSON 文件:

string data = File.ReadAllText("../../../testFiles/create.json");
string schemaText = File.ReadAllText("../../../schemas/request-payload.schema.json");
var serializer = new JsonSerializer();
var json = JsonValue.Parse(data);
var schema = serializer.Deserialize<JsonSchema>(JsonValue.Parse(schemaText));
var result = schema.Validate(json);
Assert.IsTrue(result.IsValid);

断言失败,因为 result.IsValid 是 false(这是正确的 - 我的 JSON 中存在故意错误)但没有迹象表明错误发生的位置:

我的架构在 definition 部分确实有子架构。这与它有什么关系吗?我是否需要设置一些 属性 才能看到该错误信息?

更新:添加了架构和测试 JSON

我的原始模式有几百行长,但我将其缩减为仍然存在问题的子集。这是架构:

{
    "$schema": "https://json-schema.org/draft/2019-09/schema#",
    "$id": "request-payload.schema.json",
    "type": "object",
    "propertyNames": { "enum": ["template" ] },
    "required": ["template" ],
    "properties": {
        "isPrivate": { "type": "boolean" },
        "template": {
            "type": "string",
            "enum": [ "TemplateA", "TemplateB" ]}},
    "oneOf": [
        {
            "if": {
                "properties": { "template": { "const": "TemplateB" }}},
            "then": { "required": [ "isPrivate" ] }}]
}

这是一个测试 JSON 对象:

{   
      "template": "TemplateA"
}

上面的 JSON 验证正常。将值切换为 TemplateB,JSON 验证失败(因为缺少 isPrivate 而 TemplateB 需要它),但结果不包含有关失败原因的任何信息。

上面列出了我用来运行验证测试的代码

问题可能是您没有设置输出格式。 The default format is flag 这意味着您只会得到一个 true/false 值是否通过。

要获取更多详细信息,您需要使用不同的格式设置。您可以通过 schema options.

例如:

JsonSchemaOptions.OutputFormat = SchemaValidationOutputFormat.Detailed;

可用的选项有here