JSON-schema,如何要求具有特定值的键,否则要求具有任何值的不同键

5jvtdoz2  于 2022-12-20  发布在  其他
关注(0)|答案(1)|浏览(131)

因此架构的要求是,

1.必须有“must_1”,
1.必须有其中之一;

  • “key_2”,值为真(布尔值)
  • “密钥_3”

1.当“key_2”的值为false或不存在时,“key_3”必须存在
1.可能有“optional_4”(这实际上是相当多的额外键,所有这些都将被描述)
1.不能有任何未描述的键
所以这些应该可以通过

"pass_1": {
    "must_1": 1,
    "key_2": false,
    "key_3": "ewe"
},
"pass_2": {
    "must_1": 2,
    "key_3": "ewe"
},
"pass_3": {
    "must_1": 3,
    "key_2": true
},

这应该会失败

"fail_1": {
    "must_1": 7,
    "key_2": false
},

□ □ □目前为止我的尝试
然而,这通过了“fail_1”

{
    "type": "object",
    "additionalProperties": false,
    "required": ["must_1"],
    "dependentRequired": {
        "not": {
            "key_2": ["key_3"]
        }
    },
    "properties": {
        ...
    }
}

这个真的难倒我了。

复制并粘贴代码(架构和测试)

我一直使用jsonschemalint作为比较工具

// My current none-working schema
{
    "type": "object",
    "patternProperties": {
        "[^\\s]+": {
            "type": "object",
            "additionalProperties": false,
            "required": ["must_1"],
            "dependentRequired": {
                "not": {
                    "key_2": ["key_3"]
                }
            },
            "properties": {
                "must_1": {
                    "type": "number"
                },
                "key_2": {
                    "type": "boolean"
                },
                "key_3": {
                    "type": "string"
                },
                "optional_4": {}

            }
        }
    }
}

// Tests, tests with "pass_#" should pass, tests with "fail_#" should fail :)
// 1-6 pass tests, 1-7 fail tests

{
    "pass_1": {
        "must_1": 1,
        "key_2": false,
        "key_3": "ewe"
    },
    "pass_2": {
        "must_1": 2,
        "key_3": "ewe"
    },
    "pass_3": {
        "must_1": 3,
        "key_2": true
    },
    "pass_4": {
        "must_1": 4,
        "key_2": false,
        "key_3": "ewe",
        "optional_4": "optional"
    },
    "pass_5": {
        "must_1": 5,
        "key_3": "ewe",
        "optional_4": "optional"
    },
    "pass_6": {
        "must_1": 6,
        "key_2": true,
        "optional_4": "optional"
    },
    "fail_1": {
        "must_1": 7,
        "key_2": false
    },
    "fail_2": {
        "must_1": 1,
        "key_2": false,
        "key_3": "ewe",
        "undescribed_5": "undescribed"
    },
    "fail_3": {
        "must_1": 2,
        "key_3": "ewe",
        "undescribed_5": "undescribed"
    },
    "fail_4": {
        "must_1": 3,
        "key_2": true,
        "undescribed_5": "undescribed"
    },
    "fail_5": {
        "must_1": 4,
        "key_2": false,
        "key_3": "ewe",
        "optional_4": "optional",
        "undescribed_5": "undescribed"
    },
    "fail_6": {
        "must_1": 5,
        "key_3": "ewe",
        "optional_4": "optional",
        "undescribed_5": "undescribed"
    },
    "fail_7": {
        "must_1": 6,
        "key_2": true,
        "optional_4": "optional",
        "undescribed_5": "undescribed"
    }
}
piwo6bdm

piwo6bdm1#

我得到的解决方案通过了所有测试。可能不是最好的实现

{
    "type": "object",
    "additionalProperties": false,
    "required": ["must_1"],
    "if": {
        "properties": {
            "key_2": {
                "const": false
            }
        }
    },
    "then": {
        "required": ["key_3"]
    },
    "properties": {
        ...
    }

相关问题