【发布时间】:2016-11-03 11:04:21
【问题描述】:
架构适用于具有标头属性和msg1 或msg2 属性的消息:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"definitions": {
"header": {
"type": "object",
"properties": {
"token": { "type": "string" },
"id": { "type": "number" }
},
"required": ["token", "id"]
},
"msg1": {
"type": "object",
"properties": {
"content1": { "type": "string" }
},
"required": ["content1"]
},
"msg2": {
"type": "object",
"properties": {
"content2": { "type": "string" }
},
"required": ["content2"]
}
},
"type": "object",
"$ref": "#/definitions/header",
"oneOf": [
{"$ref": "#/definitions/msg1" },
{"$ref": "#/definitions/msg2" }
]
}
所以这应该通过:
{
"token": "abc123",
"id": 333,
"content1": "s"
}
问题在于以下通过:
{
"token": "abc123",
"id": 333
}
如何解决?
(当然,msg#s 还有很多,它们的结构不同)
【问题讨论】:
-
您使用的是什么验证器?根据规范,如果 object 有 $ref 其他关键字应该被忽略(并且你在顶层有 $ref )。所以可能 oneOf 被忽略了。但是不同的验证器实现它的方式不同。
-
@esp,没错。
标签: json validation jsonschema