简体   繁体   English

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

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

I assume I've missed something in defining the schema.我假设我在定义模式时遗漏了一些东西。 Note that the id property is required under the form property, but it is not provided in the JSON, yet the JSON validates correctly according to multiple JSON validators online.注意form属性下需要id属性,但是JSON中没有提供,但是JSON根据多个在线JSON验证器正确验证。

Schema架构

{
  "$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 Payload JSON 有效载荷

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

Your schema doesn't specify which properties are required .您的架构没有指定required哪些属性。 You need to set required to the fields you want to be there.您需要将required设置为您想要在那里的字段。 You may also wish to set additionalProperties to false .您可能还希望将additionalProperties设置为false

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

I don't see nullable as a key anywhere in the json schema docs.我在 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