Json Schema Conditional if/Then not working



我是Json架构验证的新手。我认为验证应该失败,但它通过了。不确定为什么If/then没有强制输入所需字段。我相信我对If/Then的格式设置正确。

JSON:

{
"name": "Battery Wear",
"triggerAlert": {
"trigger": "When",
"timeSpan": 50,
"timeSpanMeasure": "Hours"
}
}

方案:

{
"$schema": "http://json-schema.org/draft-07/schema",
"type": "object",
"required": [
"name",
"triggerAlert"
],
"properties": {
"name": {
"type": "string"
},
"triggerAlert": {
"type": "object",
"required": ["trigger"],
"properties": {
"trigger": {
"type": "string",
"enum": ["Always","When"]
},
"numberOfEvents": {
"type": "integer"
},
"timeSpan": {
"type": "integer"
},
"timeSpanMeasure": {
"type": "string"
}
},
"if": { "properties": {"trigger": {"enum": ["When"]} } },
"then": {            
"required": [
"numberOfEvents",
"timeSpan",
"timeSpanMeasure"
]
}
}
}
}

根据您正在使用的实现,您可能不支持这些条件。if/then/else仅在规范草案7中添加。

架构是正确的;预期的错误结果是:

{
"errors" : [
{
"error" : "missing property: numberOfEvents",
"instanceLocation" : "/triggerAlert",
"keywordLocation" : "/properties/triggerAlert/then/required"
},
{
"error" : "subschema is not valid",
"instanceLocation" : "/triggerAlert",
"keywordLocation" : "/properties/triggerAlert/then"
},
{
"error" : "not all properties are valid",
"instanceLocation" : "",
"keywordLocation" : "/properties"
}
],
"valid" : false
}

相关内容

  • 没有找到相关文章

最新更新