简体   繁体   中英

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.

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

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

Your schema doesn't specify which properties are required . You need to set required to the fields you want to be there. You may also wish to set additionalProperties to false .

Docs: 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.

{
  "$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
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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