【问题标题】:JSON Schema oneOf doesn't work with referencesJSON Schema oneOf 不适用于引用
【发布时间】:2016-11-03 11:04:21
【问题描述】:

架构适用于具有标头属性和msg1msg2 属性的消息:

{
    "$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


【解决方案1】:

$ref 以外的所有内容都将被忽略。

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

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

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

【讨论】:

    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    • 2017-06-24
    • 2014-03-01
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多