繁体   English   中英

即使缺少字段,JSON 有效负载也会根据架构进行验证

[英]JSON payload validates against schema even though field is missing

我假设我在定义模式时遗漏了一些东西。 注意form属性下需要id属性,但是JSON中没有提供,但是JSON根据多个在线JSON验证器正确验证。

架构

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.biz",
  "type": "object",
  "properties": {
    "form": {
      "type": "object",
      "nullable": false,
      "properties": {
        "id": {
          "type": "number",
          "description": "The unique identifier of the form.",
          "nullable": false
        },
        "name": {
          "type": "string",
          "description": "The name of the form.",
          "nullable": false
        }
      }
    }
  }
}

JSON 有效载荷

{
  "form": {
    "name": "Test 2"
  }
}

您的架构没有指定required哪些属性。 您需要将required设置为您想要在那里的字段。 您可能还希望将additionalProperties设置为false

文档: https://json-schema.org/understanding-json-schema/reference/object.html#required-properties

我在 json 架构文档中的任何地方都没有将nullable视为键。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://example.biz",
  "type": "object",
  "properties": {
    "form": {
      "type": "object",
      "properties": {
        "id": {
          "type": "number",
          "description": "The unique identifier of the form."
        },
        "name": {
          "type": "string",
          "description": "The name of the form."
        }
      },
      "required": ["id", "name"],
      "additionalProperties": false
    }
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM