"Not""required"组合

"Not" "required" combination

我已经用一些属性定义了架构...根据条件,我想禁用/启用这些属性...我也需要一些默认行为... 注意:"additionalProperties" 将始终为假。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/TestProperties",
  "TestProperties": {
    "type": "object",
    "additionalProperties": false,
    "properties": {
      "A": { "$ref": "#/valueDefinition/stringObject" },
      "B": { "$ref": "#/valueDefinition/stringObject" },
      "C": { "$ref": "#/valueDefinition/stringObject" },
      "D": { "$ref": "#/valueDefinition/stringObject" },
      "E": { "$ref": "#/valueDefinition/stringObject" }
    },
    "if": {
      "properties": {
        "A": { "const": "Start" }
      }
    },
    "then": {
      "not": { "required": ["C", "D", "E"] },
      "required": ["B"]
    },
    "else": {
      "if": {
        "properties": {
          "taskType": { "const": "Stop" }
        }
      },
      "then": {
        "not": { "required": ["D", "E"] },
        "required": ["B", "C"]
      },
      "else": {
        "not": { "required": ["B", "C"] },
        "required": ["D", "E"]
      }
    }
  },
  "valueDefinition": {
    "stringObject": {
      "type": "string",
      "pattern": "^(?!\s*$).+"
    }
  }
}

如果我的输入是

{
  "A":"Start",
},

正在抛出错误,提示缺少强制输入...

如果我的输入是

{
  "A":"Start",
  "B":"xxxx"
}

显示有效JSON...

但是如果我的输入是

{
  "A":"Start",
  "B":"xxxx",
  "C":"yyyy"
}

它没有抛出任何错误...我的期望是它不应该容纳 C

仅当 "C"、"D" 和 "E" 都不存在时,以下内容才有效。

"not": { "required": ["C", "D", "E"] },

如果 "C"、"D" 或 "E" 中的任何一个不存在,您希望它有效。

"not": {
  "anyOf": [
    { "required": ["C"] },
    { "required": ["D"] },
    { "required": ["E"] }
  ]
}