1

我是关于 npm ajv 的新手

我有一个问题:对象 json 中有多少个“开关”?例子:

var schema = {
  "type": "object",
    "switch": [
        {
            "if": {
                "properties": {
                  "powerLevel": {"constant": false}
                }
            },
            "then": {
                "required": ["disbelief"]
            }
        },
        {
            "then": {
                "required": ["confidence"]
            }
        }
    ],
    "switch": [
        {
            "if": {
                "properties": {
                  "power": {"constant": false}
                }
            },
            "then": {
                "required": ["disb"]
            }
        },
        {
            "then": {
                "required": ["conf"]
            }
        }
    ]
};

我在此链接中使用上面的架构进行测试

它只是检查结束开关。请帮我!谢谢!

4

1 回答 1

1

您不能在同一个对象中切换两个关键字。

在此特定情况下,您可以在一个开关中合并“案例”:

{
    "type": "object",
    "switch": [
        {
            "if": { "properties": { "powerLevel": {"constant": false} } },
            "then": { "required": ["disbelief"] }
        },
        {
            "if": { "properties": { "power": {"constant": false} } },
            "then": { "required": ["disb"] }
        },
        {
            "then": {
                "oneOf": [
                    { "required": ["confidence"] },
                    { "required": ["conf"] }
                ]
            }
        }
    ]
}

在一般情况下,您可以使用关键字 allOf、anyOf、oneOf 来合并两个包含重复关键字的模式。

于 2016-09-14T22:39:47.600 回答