首页 > 解决方案 > 使用 Newtonsoft 在 C# 中使用 JSON Schema 验证 JSON

问题描述

使用 JSON Schema 验证 JSON 始终返回 true。Newtonsoft 用于验证,并在此处使用模式和数据进行测试。它总是返回'未发现错误。JSON 验证架构'。

请找到我的 JSON 架构。


{
  "schema": {
    "definitions": {
    },
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://example.com/root.json",
    "type": "object",
    "widget": { "formlyConfig": { "type": "accordion" } },
    "title": "The Root Schema",
    "required": [
      "accordion1",
      "accordion2",
      "accordion3"
    ],
    "properties": {
      "accordion1": {
        "$id": "#/properties/accordion1",
        "type": "object",
        "title": "The Accordion1 Schema",
        "required": [
          "firstname",
          "age"
        ],
        "properties": {
          "firstname": {
            "$id": "#/properties/accordion1/properties/firstname",
            "type": "string",
            "title": "The Firstname Schema",
            "default": "firstname pvr1"
          },
          "age": {
            "$id": "#/properties/accordion1/properties/age",
            "type": "integer",
            "title": "The Age Schema",
            "default": 21
          }
        }
      },
      "accordion2": {
        "$id": "#/properties/accordion2",
        "type": "object",
        "title": "The Accordion2 Schema",
        "required": [
          "firstname",
          "age"
        ],
        "properties": {
          "firstname": {
            "$id": "#/properties/accordion2/properties/firstname",
            "type": "string",
            "title": "The Firstname Schema",
            "default": "firstName2"
          },
          "age": {
            "$id": "#/properties/accordion2/properties/age",
            "type": "integer",
            "title": "The Age Schema",
            "default": 31
          }
        }
      },
      "accordion3": {
        "$id": "#/properties/accordion3",
        "type": "object",
        "title": "The Accordion3 Schema",
        "required": [
          "firstname",
          "age"
        ],
        "properties": {
          "firstname": {
            "$id": "#/properties/accordion3/properties/firstname",
            "type": "string",
            "title": "The Firstname Schema",
            "default": "firstnaem3"
          },
          "age": {
            "$id": "#/properties/accordion3/properties/age",
            "type": "integer",
            "title": "The Age Schema",
            "default": 10
          }
        }
      }
    },
      'additionalProperties': false
  }
}


请找到 JSON

{ 
   "accordion1":{ 
      "firstname":"JSON ACCORD PALANIVELRAJAN",
      "age":29
   },
   "accordion2":{ 
      "firstname":"JSON ACCORD LAKSHMANAN",
      "age":39
   },
   "accordion3":{ 
      "firstname":null,
      "age":49
   }
}

我试图将名字更改为整数并删除手风琴1中的第一个。它在所有情况下都返回 true。

请指教。

请找到使用 JSON Schema 验证 JSON 的代码。

model 是一个 JObject,它是一个有效的 JSON。

JsonSchema json_schema = JsonSchema.Parse(schema);
IList<string> messages;
bool valid = model.IsValid(json_schema, out messages);
return valid;

标签: c#jsonjson.netjsonschema

解决方案


JsonSchema已弃用,并已移至单独的包:Newtonsoft.Json.Schema。使用这个包,我能够根据您的架构验证您的 JSON(我确实删除了外部schema元素,因为它实际上是无效的,并导致架构无法正确验证 - 我认为您可能已经在那里拥有它,因为旧JsonSchema类否则无法解析模式!),如果我将 JSON 更改为无效形状、删除所需元素或将数据更改为无效类型,则会收到错误消息:

            string data = File.ReadAllText("data.json");
            string schema = File.ReadAllText("data.schema.json");

            var model = JObject.Parse(data);
            var json_schema = JSchema.Parse(schema);

            IList<string> messages;
            bool valid = model.IsValid(json_schema, out messages); // properly validates

我正在使用 .NET Core 2.2、Newtonsoft.Json 12.0.2 和 Newtonsoft.Json.Schema 3.0.11,以防万一。请注意,Newtonsoft.Json.Schema 包对商业用途有限制 - 检查许可!


推荐阅读