【发布时间】:2021-12-05 21:04:10
【问题描述】:
我的架构有一个可选的 boolean 属性,名为 prepaid。还有三个可选的数字属性。如果提供了预付属性并且值为true,那么我希望需要其他三个属性。如果我在 JSON 中明确包含预付费,它工作正常,但如果我将其全部省略,那么其他三个仍然需要。这是我的架构。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "https://example.com/object1638395656.json",
"type": "array",
"items": {
"type": "object",
"required": [
"id",
"name"
],
"properties": {
"id": {
"type": "string",
"format": "uuid"
},
"name": {
"type": "string",
"minLength": 1
},
"overageRate": {
"type": "number",
"minimum": 0
},
"pointsPerMonth": {
"type": "number",
"minimum": 0
},
"prepaid": {
"type": "boolean"
},
},
"if": {
"properties": {
"prepaid": {
"const": true
}
}
},
"then": {
"required": [
"pointsPerMonth",
"monthlyRate",
"overageRate"
]
}
}
}
另外,有没有办法说如果 prepaid 丢失或设置为 false,那么 JSON 中根本不应该允许这三个属性?
【问题讨论】:
标签: jsonschema