我有一个要定义的对象,需要一个键或另一个特定的键。 基本上是一个简单的或:if(key1 || key2){->有效} else {->无效}
现在我知道了“ require”关键字,但是据我所知,它不能有条件地要求键。 就我而言,我想完全将key1或key2存在。
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "/my_schema",
"type": "object",
"properties": {
"key1": {
"type": "string"
},
"key2": {
"type": "array",
"items": {
"type": "string"
}
},
"key3": {
"type": "array",
"items": {
"type": "string",
"minItems": 1
}
},
"key4": {
"type": "object",
"properties": {
"begin": {
"type": "number"
},
"end": {
"type": "number"
}
}
}
},
"required": [
"key3", "key4"
]
}
如果可能的话,我会寻找一些可以有条件地定义对象的东西,例如:
"required": {
"key1": if(someConditions),
"key2": if(someOtherConditions)
}
甚至是:
"porperties":{
"key1":{
"type": someType,
"required": if(someCondition)
}
}
但是现在,我只想确保需要key1或key2,并且如果两个都缺失,则该架构无效。
答案 0 :(得分:1)
要验证至少需要两个属性之一,可以使用<body>
<form>
<div class="range-slider">
<input class="range-slider__range" type="range" name="amountRange" min="0" max="1000" value="0" oninput="this.form.amountInput.value=this.value" />
<input class="output" type="number" name="amountInput" min="0" max="1000" value="0" oninput="this.form.amountRange.value=this.value" />
<span class="range-slider__value">0</span>
</div>
</form>
<form>
<div class="range-slider">
<input class="range-slider__range" type="range" name="amountRange" min="0" max="1000" value="0" oninput="this.form.amountInput.value=this.value" />
<input class="output-two" type="number" name="amountInput" min="0" max="1000" value="0" oninput="this.form.amountRange.value=this.value" />
<span class="range-slider__value">0</span>
</div>
</form>
<div class="final-outputs">
<div class="final-output final-output--add">
<h5>addition</h5><span class="addition">1</span></div>
<div class="final-output final-output--sub">
<h5>subtraction</h5><span class="subtraction">2</span></div>
<div class="final-output final-output--div">
<h5>division</h5><span class="division">3</span></div>
</div>
</body>
这样的块。
anyOf
要基于某些条件验证可用性,可以使用"properties": {...},
"required":[
"key3",
"key4"
],
"anyOf": [
{
"required": [
"key1"
]
},
{
"required": [
"key2"
]
}
]
if
then
关键字。例如,
如果key1 =“ 1”,则需要key2。否则,必须输入key3
else