JSON 架构验证 - 必填字段取决于布尔值

JSON Schema Validation - required fields depending on boolean value

我看了很多其他帖子,但找不到解决我问题的方法。

我正在尝试通过 json 模式验证以下场景:

如果 isRetired = false 则额外需要 retirementAge、salary 和 salaryFreq。

除了带有 2 个模式的 oneOf 之外,我还尝试了以下方法以包含 isRetired = true/false 的所有必需字段,但这也没有用。

如有任何建议,我们将不胜感激。

提前致谢!

{
    "definitions": {},
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "additionalProperties": false,
    "required": ["input"],
    "properties": {
      "input": {
        "$id": "#root/input",
        "title": "Input",
        "type": "object",
        "required": ["persons"],
        "properties": {
          "persons": {
            "$id": "#root/input/persons",
            "title": "Persons",
            "type": "array",
            "default": [],
            "minItems": 1,
            "maxItems": 2,
            "items": {
              "$id": "#root/input/persons/items",
              "title": "Items",
              "type": "object",
              "required": [
                "gender",
                "age",
                "isRetired",
                "superConcContPercentage",
                "totalSuper",
                "riskGrowthPercentage",
                "includeAgePension"
              ],
              "properties": {
                "firstName": {
                  "$id": "#root/input/persons/items/firstName",
                  "title": "Firstname",
                  "type": "string",
                  "default": "",
                  "examples": ["Joe"],
                  "pattern": "^.*$"
                },
                "lastName": {
                  "$id": "#root/input/persons/items/lastName",
                  "title": "Lastname",
                  "type": "string",
                  "default": "",
                  "examples": ["Bloggs"],
                  "pattern": "^.*$"
                },
                "gender": {
                  "$id": "#root/input/persons/items/gender",
                  "title": "Gender",
                  "type": "string",
                  "default": "",
                  "examples": ["male"],
                  "pattern": "^.*$"
                },
                "age": {
                  "$id": "#root/input/persons/items/age",
                  "title": "Age",
                  "type": "integer",
                  "examples": [27],
                  "default": 0,
                  "minimum": 18,
                  "maximum": 110
                },
                "isRetired": {
                  "$id": "#root/input/persons/items/isRetired",
                  "title": "Isretired",
                  "type": "boolean",
                  "examples": [false],
                  "default": true
                },
                "retirementAge": {
                  "$id": "#root/input/persons/items/retirementAge",
                  "title": "Retirementage",
                  "type": "integer",
                  "examples": [70],
                  "default": 0,
                  "minimum": 19,
                  "maximum": 110
                },
                "salary": {
                  "$id": "#root/input/persons/items/salary",
                  "title": "Salary",
                  "type": "integer",
                  "examples": [100000],
                  "default": 0,
                  "minimum": 0
                },
                "salaryFreq": {
                  "$id": "#root/input/persons/items/salaryFreq",
                  "title": "Salaryfreq",
                  "type": "integer",
                  "examples": [12],
                  "default": 0
                },
                "superConcContPercentage": {
                  "$id": "#root/input/persons/items/superConcContPercentage",
                  "title": "Superconccontpercentage",
                  "type": "integer",
                  "examples": [0],
                  "default": 0,
                  "minimum": 0,
                  "maximum": 100
                },
                "totalSuper": {
                  "$id": "#root/input/persons/items/totalSuper",
                  "title": "Totalsuper",
                  "type": "integer",
                  "examples": [10000],
                  "default": 0,
                  "minimum": 0
                },
                "riskGrowthPercentage": {
                  "$id": "#root/input/persons/items/riskGrowthPercentage",
                  "title": "Riskgrowthpercentage",
                  "type": "integer",
                  "examples": [50],
                  "default": 0,
                  "minimum": 0,
                  "maximum": 100
                },
                "includeAgePension": {
                  "$id": "#root/input/persons/items/includeAgePension",
                  "title": "Includeagepension",
                  "type": "boolean",
                  "examples": [false],
                  "default": true
                }
              }
            },
            "if": {
              "properties": {
                "isRetired": {
                  "const": false
                }
              }
            },
            "then": {
              "required": [
                "retirementAge",
                "salary",
                "salaryFreq"
              ]
            },
            "else": {
              "required": []
            }
          }
        }
      }
    }
  }

我在尝试为自己弄清楚这个确切用例时发现了您的问题。这是我想出的:

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "type": "object",
    "properties": {
        "trigger": { "type": "boolean" },
        "dependentProp": { "type": "string" }
    },
    "if": {
        "properties": {
            "trigger": {
              "type": "boolean",
              "enum": [false]
            }
        }
    },
    "then": { "required": ["dependentProp"] }
}

可能有更简洁的方法来完成它,但它对我有用,所以我继续使用它并继续前进哈哈。

我最终得到以下满足用例的结果:

"if": {
  "properties": {
    "isRetired": {
      "const": false
    }
  }
},
"then": {
  "properties": {
    "salary": {
      "$id": "#root/input/persons/items/salary",
      "title": "Salary",
      "type": "integer",
      "examples": [100000],
      "default": 0,
      "minimum": 0
    },
    "salaryFreq": {
      "$id": "#root/input/persons/items/salaryFreq",
      "title": "Salaryfreq",
      "type": "integer",
      "examples": [12],
      "default": 0
    }
  },
  "required": ["salary", "salaryFreq"]
}