2

使用 6.5 版

我的问题是我有一个请求对象,该对象必须包含一个属性,并且应该包含属性列表中的一个且仅包含一个其他属性,或者没有那些指定的属性。我已经进行了一些挖掘,但无法找到解决此问题的方法。

可能的有效请求:

{ 'query': {...}, 'limit': 1 }
{ 'query': {...}, 'count': true }
{ 'query': {...}, 'max': 'my_string' }

无效的请求是:

{ 'query': {...}, 'limit': 1, 'count': true }

或者

{ 'query': {...}, 'max': 'my_string', limit: 1 }

等等

我想出的最好的 ajv 验证器对象如下:

{
    "type": "object",
    "required": ["query"],
    "maxProperties": 2,
    "properties": {
        "query": {
            "type": "object"
        },
        "limit": {
            "type": "integer"
        },
        "count": {
            "type": "boolean"
        },
                "max": {
                        "type": "string"
                }
    }
}

但我知道这不会随着我们应用程序的增长而扩展。我想知道是否有办法指定对象需要“查询”和“限制”、“计数”、“最大值”的 ONE 或 NONE。

4

1 回答 1

1

找到了一种方法来完成我正在寻找的东西。这可以使用“依赖”验证器来完成:

"dependencies": {
    "limit": { "properties": 
        { 
            "count": { "not": {} },
            "max": { "not": {} }
        }
    },
    "count": { "properties": 
        { 
            "limit": { "not": {} },
            "max": { "not": {} }
        }
    },
    "max": { "properties": 
        { 
            "limit": { "not": {} },
            "count": { "not": {} }
        }
    }
}

如果有人知道更好的方法,我很想知道!

于 2018-05-10T19:16:34.887 回答