JSON模式oneOf不';我不需要参考资料

JSON模式oneOf不';我不需要参考资料,json,validation,jsonschema,Json,Validation,Jsonschema,架构用于消息头属性,然后是msg1或msg2的属性: { "$schema": "http://json-schema.org/draft-04/schema#", "definitions": { "header": { "type": "object", "properties": { "token": { "type": "string" },

架构用于消息头属性,然后是
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,它们有不同的结构)

除了
$ref
之外的所有内容都被忽略了

JSON引用对象中除“$ref”之外的任何成员都将被忽略

您可以通过将
$ref
包装在
allOf
中来解决此问题

"allOf": [{"$ref": "#/definitions/header"}],

您正在使用什么验证器?按规范,如果对象有$ref,则应忽略其他关键字(并且在顶层有$ref)。因此,其中一个可能被忽略了。但是不同的验证器实现它的方式不同。@esp,没错。