使用6.5版
我的问题是,我有一个request对象,它必须包含一个属性,并且应该包含属性列表中的一个且只能包含一个其他属性,或者不包含任何指定的属性。我已经做了一些调查,但一直无法找到解决这个问题的方法。
可能的有效请求:
{ '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"
}
}
}但我知道这不会随着我们的应用程序的增长而扩展。我想知道是否有一种方法可以指定对象需要"query“以及"limit”、"count“、"max”中的一个或不需要。
发布于 2018-05-11 03:16:35
找到了一种方法来完成我正在寻找的东西。这可以使用“依赖”验证器来完成:
"dependencies": {
"limit": { "properties":
{
"count": { "not": {} },
"max": { "not": {} }
}
},
"count": { "properties":
{
"limit": { "not": {} },
"max": { "not": {} }
}
},
"max": { "properties":
{
"limit": { "not": {} },
"count": { "not": {} }
}
}
}如果有人知道更好的方法,我很想知道!
https://stackoverflow.com/questions/50278812
复制相似问题