Node.js 节点AJV验证模式
使用版本6.5 我的问题是我有一个请求对象,它必须包含一个属性,并且应该从属性列表中只包含一个属性,或者不包含任何指定属性。我已经做了一些挖掘,一直无法找到解决这个问题的办法 可能的有效请求:Node.js 节点AJV验证模式,node.js,jsonschema,json-schema-validator,ajv,Node.js,Jsonschema,Json Schema Validator,Ajv,使用版本6.5 我的问题是我有一个请求对象,它必须包含一个属性,并且应该从属性列表中只包含一个属性,或者不包含任何指定属性。我已经做了一些挖掘,一直无法找到解决这个问题的办法 可能的有效请求: { 'query': {...}, 'limit': 1 } { 'query': {...}, 'count': true } { 'query': {...}, 'max': 'my_string' } 无效的请求可能是: { 'query': {...}, 'limit': 1, 'count':
{ 'query': {...}, 'limit': 1 }
{ 'query': {...}, 'count': true }
{ 'query': {...}, 'max': 'my_string' }
无效的请求可能是:
{ 'query': {...}, 'limit': 1, 'count': true }
或
等等
我提出的最好的ajv验证器对象如下:
{
"type": "object",
"required": ["query"],
"maxProperties": 2,
"properties": {
"query": {
"type": "object"
},
"limit": {
"type": "integer"
},
"count": {
"type": "boolean"
},
"max": {
"type": "string"
}
}
}
但我知道这不会随着应用程序的增长而扩展。我想知道是否有一种方法可以指定对象需要“查询”,并且“限制”、“计数”、“最大值”中的一个或任何一个都不能找到一种方法来完成我正在寻找的内容。这可以使用“依赖性”验证器来完成:
"dependencies": {
"limit": { "properties":
{
"count": { "not": {} },
"max": { "not": {} }
}
},
"count": { "properties":
{
"limit": { "not": {} },
"max": { "not": {} }
}
},
"max": { "properties":
{
"limit": { "not": {} },
"count": { "not": {} }
}
}
}
如果有人知道更好的方法,我很想知道
"dependencies": {
"limit": { "properties":
{
"count": { "not": {} },
"max": { "not": {} }
}
},
"count": { "properties":
{
"limit": { "not": {} },
"max": { "not": {} }
}
},
"max": { "properties":
{
"limit": { "not": {} },
"count": { "not": {} }
}
}
}