【问题标题】:JSON schema - how to specify permutations of required propertiesJSON 模式 - 如何指定所需属性的排列
【发布时间】:2015-04-01 21:07:33
【问题描述】:

以下是我的 JSON 架构的摘录。

我想指定algorithm 和results 都是必需的。

另外我想说明:

  • 当algorithm 为algorithm1 时,结果必须是results1、results2 或results3 之一。
  • 当algorithm 为algorithm2 时,结果必须是results2、results3 或results4 之一。

这可能吗?

        "algorithm": {
            "description": "description...",
            "type": "string",
            "enum": [
                "",
                "algorithm1",
                "algorithm2"
            ]
        },
        "results": {
            "description": "description...",
            "type": "string",
            "enum": [
                "",
                "results1",
                "results2",
                "results3",
                "results4"
            ]
        },
     "required": ["algorithm", "results"]

【问题讨论】:

标签: json schema jsonschema


【解决方案1】:

感谢@jruizaranguren 提供的上述参考资料,我能够弄清楚。

"required": ["results"],
"results": {
    "type": "object",
    "oneOf": [
        { "$ref": "#/definitions/Results1" },
        { "$ref": "#/definitions/Results2" }
    ]
},
"definitions": {
    "Results1": {
        "type": "object",
        "required": ["algorithm", "results"],
        "properties": {
            "algorithm": {
                "type": "string",
                "enum": [ "algorithm1" ]
            },
            "results": {
                "type": "string",
                "allOf": [
                    { "result": "results1" },
                    { "result": "results2" },
                    { "result": "results3" }
                ]
            }
        }
    },
    "Results2": {
        "type": "object",
        "required": ["algorithm", "results"],
        "properties": {
            "algorithm": {
                "type": "string",
                "enum": [ "algorithm2" ]
            },
            "results": {
                "type": "string",
                "allOf": [
                    { "result": "results2" },
                    { "result": "results3" },
                    { "result": "results4" }
                ]
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 2012-07-28
    • 2015-10-28
    相关资源
    最近更新 更多