【发布时间】:2018-05-09 13:19:20
【问题描述】:
我一直在使用 json 架构来验证来自我的一个网络服务的答案。
答案分为两个属性:data 和 status。如果 status.code 设置为 0,则 data 将必须遵守特定模式。否则,如果 status.code 设置为 -1,data 将不会被读取,所以我不想检查它是否尊重架构。
这是架构:
{
"$schema": "http://json-schema.org/schema#",
"id": "file://registration.json",
"type": "object",
"properties": {
"status": {
"$ref": "#/definitions/classes/status"
}
},
"anyOf": [
{
"$ref": "#/definitions/conditions/status-is-ok"
},
{
"$ref": "#/definitions/conditions/status-is-nok"
}
],
"definitions": {
"classes": {
"status": {
"type": "object",
"properties": {
"code": {
"type": "integer"
},
"message": {
"type": "string"
}
},
"required": [
"code",
"message"
]
},
"data": {
"type": "object",
"properties": {
"propertyA": {
"type": "#/definitions/classes/metadatauser"
},
"propertyB": {
"type": "#/definitions/classes/membreinfo"
}
},
"required": ["propertyA", "propertyB"]
}
},
"conditions": {
"status-is-ok": {
"status": {
"properties": {
"code": 0
}
},
"data": {
"$ref": "#/definitions/classes/data"
}
},
"status-is-nok": {
"status": {
"properties": {
"code": -1
}
},
"data": {
"type": "object"
}
}
}
}
}
以下是不应验证的示例:
{
"data": {},
"status": {
"code": 0,
"message": "OK"
}
}
目前这部分代码通过了,不知道为什么。
【问题讨论】:
标签: jsonschema