在下面的模型中,仅当“详细信息”数组为空时,才需要“ category_id”属性。
如果“详细信息”数组不为空,则不需要“ category_id”属性。
如何使用JSON模式执行此操作?
{
"description": "Expense model validation.",
"type": "object",
"properties": {
"description": {
"type": "string"
},
"category_id": {
"type": "string"
},
"detail": {
"type": "array",
"items": {
"description": "Expense detail",
"type": "object",
"properties": {
"description": {
"type": "string"
}
},
"required": [ "description" ]
}
}
},
"required": [ "description", "category_id" ]
}
您可以anyOf
用来检查category_id
是否存在,或者detail
存在并且至少有一项。
{
"description": "Expense model validation.",
"type": "object",
"properties": {
"description": { "type": "string" },
"category_id": { "type": "string" },
"detail": {
"type": "array",
"items": {
"description": "Expense detail",
"type": "object",
"properties": {
"description": { "type": "string" }
},
"required": ["description"]
}
}
},
"required": ["description"],
"anyOf": [
{ "required": ["category_id"] },
{
"properties": {
"detail": { "minItems": 1 }
},
"required": ["detail"]
}
]
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句