JSON模式条件:根据另一个字段的值,字段是必需的

JSON模式条件:根据另一个字段的值,字段是必需的,json,jsonschema,Json,Jsonschema,我试图实现这个条件:根据另一个字段的值,字段是必需的,即,如果存在带有“index”:“true”的请求,则需要“id”元素:true 下面是一个示例模式: { "$schema": "http://json-schema.org/draft-04/schema#", "title": "test title", "type": "object", "properties": { "data": { "type": "array", "items"

我试图实现这个条件:根据另一个字段的值,字段是必需的,即,如果存在带有“index”:“true”的请求,则需要“id”元素:true

下面是一个示例模式:

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "title": "test title",
  "type": "object",
  "properties": {
    "data": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/Item"
      },
      "minItems": 0
    }
  },
  "required": [
    "data"
  ],
  "definitions": {
    "Item": {
      "type": "object",
      "properties": {
        "id": {
          "type": [
            "integer",
            "string"
          ]
        },
        "type": {
          "type": "string"
        }
      }
    }
  }
}
如何实施


任何指针都会有帮助。

您的示例chema对我来说很奇怪,但对于票据描述中的验证案例,您可以使用json shema的这一部分:

"properties": {
    "data": {
        "oneOf": [
            {
                "type": "object",
                "required": [
                    "index"
                ],
                "properties": {
                    "index": {
                        "type": "boolean",
                        "enum": [false]
                    }
                }
            },
            {
                "type": "object",
                "required": [
                    "index",
                    "id"
                ],
                "properties": {
                    "index": {
                        "type": "boolean",
                        "enum": [true]
                    },
                    "id": {
                        "type": "boolean"
                    }
                }
            }
        ]
    }
}
当其他参数等于某个值时,如果要验证一个参数,此技巧将对您有所帮助。

可能重复的参数的可能重复