当前位置: 动力学知识库 > 问答 > 编程问答 >

JSON schema - how to specify permutations of required properties

问题描述:

Below is an extract from my JSON schema.

I want to specify that both algorithm and results are required.

Additionally I want to specify:

  • when algorithm is algorithm1, then results must be one of results1, results2, or results3.
  • when algorithm is algorithm2, then results must be one of results2, results3, or results4.

Is this possible?

 "algorithm": {

"description": "description...",

"type": "string",

"enum": [

"",

"algorithm1",

"algorithm2"

]

},

"results": {

"description": "description...",

"type": "string",

"enum": [

"",

"results1",

"results2",

"results3",

"results4"

]

},

"required": ["algorithm", "results"]

网友答案:

Thanks to the reference above from @jruizaranguren, I was able to figure it out.

"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" }
                ]
            }
        }
    }
分享给朋友:
您可能感兴趣的文章:
随机阅读: