如何在 json-schema 中的 "then" 中表达项目属性?

how to express an item attribute in the "then" in json-schema?

我有以下 json 架构:

 {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "myJsonSchema",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "flag": {
      "type": "boolean"
    },
    "myArray": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/ArrayItem"
      }
    }
  },
  "definitions": {
    "ArrayItem": {
      "type": "object",
      "properties": {
        "itemAttribute1": {
          "type": "string",
          "description": ""
        },
        "itemAttribute2": {
          "type": "string",
          "description": ""
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "flag": {
          "const": true
        }
      },
      "then": {
        "myArray": {
          "items": {
            "required": "itemAttribute1"
          }
        }
      }
    }
  ]
}

我想验证当标志设置为 true 时,字段 "itemAttribute" 是必需的。我怎样才能做到这一点?

示例:

这个json应该是有效的:

{
    "flag" : true,
      "myArray" : [
        {
          "itemAttribute1" : "c",
          "itemAttribute2" : "d"
        }
      ]
}

虽然这个 json 应该无效(因为自 flag=true 以来,itemAttribute1 成为必需的):

{
    "flag" : true,
      "myArray" : [
        {
          "itemAttribute2" : "d"
        }
      ]
}

ifthenelse 关键字将完整架构作为其值。要在其中表达对象属性,您需要将这些 属性 名称嵌套在 properties 关键字下,并在 ifthen 中嵌套。

此外,除非您计划在 allOf 下添加更多子模式,否则没有必要。

正如 gregsdennis 所解释的,ifthen 中的每一个都采用 JSON 架构作为其值。 我修改了您原始架构中的 ifthen 部分。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "myJsonSchema",
  "type": "object",
  "additionalProperties": false,
  "properties": {
    "flag": {
      "type": "boolean"
    },
    "myArray": {
      "type": "array",
      "items": {
        "$ref": "#/definitions/ArrayItem"
      }
    }
  },
  "definitions": {
    "ArrayItem": {
      "type": "object",
      "properties": {
        "itemAttribute1": {
          "type": "string",
          "description": ""
        },
        "itemAttribute2": {
          "type": "string",
          "description": ""
        }
      }
    }
  },
  "allOf": [
    {
      "if": {
        "properties": {
          "flag": {
            "const": true
          }
        }
      },
      "then": {
        "properties": {
          "myArray": {
            "items": {
              "required": ["itemAttribute1"]
            }
          }
        }
      }
    }
  ]
}