JSON模式验证-需要的字段取决于布尔值



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

我试图通过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"]
}

最新更新