Json.NET IsValid 模式传递即使不匹配

Json.NET IsValid with schema passing even though not matching

我在 draft-03 中有一个 json 架构。其中一个属性是这样的 -

"KeyToCheck": {
      "type": "array",
      "items": {
        "type": "object",
        "required": "true",
        "properties": {
          "property1": {
            "type": "string",
            "required": "true"
          },
          "property2": {
            "type": "string",
            "required": "true"
          },
          "property3": {
            "type": "number",
            "required": "true"
          },
          "property4": { "type": "array" }
        }
      },
      "required": "true"
    }

我已经为 KeyToCheck 应用了 required-true(上面最底层的 required-true json。然而,即使 KeyToCheck 数组在 Response 中变空,IsValid 方法仍然通过。该方法用于这样 - Jtoken.parse(response).isvalid(schema).

问题是,在您的 JSON 架构中,您没有验证 KeyToCheck 数组的长度。您可以使用 minItems 属性 来设置数组的下限。尝试遵循 JSON 架构。希望有用。

{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "type": "object",
  "properties": {
    "KeyToCheck": {
      "type": "array",
      "required": true,
      "minItems": 1,
      "items": {
        "type": "object",
        "required": true,
        "properties": {
          "property1": {
            "type": "string",
            "required": true
          },
          "property2": {
            "type": "string",
            "required": true
          },
          "property3": {
            "type": "number",
            "required": true
          },
          "property4": {
            "type": "array"
          }
        }
      }
    }
  }
}

输入JSON:

{
 "KeyToCheck":[]
}

验证结果:

Message: Array item count 0 is less than minimum count of 1. Schema path: #/properties/KeyToCheck/minItems

编辑:

要验证非空数组,请使用此架构

{
  "$schema": "http://json-schema.org/draft-03/schema#",
  "type": "object",
  "properties": {
    "KeyToCheck": {
      "type": "array",
      "required": true,
      "maxItems": 0
    }
  }
}

输入JSON:

{
 "KeyToCheck":["test_value"]
}

验证结果:

Message: Array item count 1 exceeds maximum count of 0. Schema path: #/properties/KeyToCheck/maxItems