簡體   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