Json-schema-如何验证特定元素不在数组中?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Json-schema-如何验证特定元素不在数组中?相关的知识,希望对你有一定的参考价值。
我有一个帐户列表(帐户ID,货币,accountType)
Id1,美元,帐户/Id2,GBP,帐户/Id3,欧元,帐户/Id4,NOK,帐户
[当我调用api以获取帐户列表时,我要确保结果中没有accountid = 3。我该如何实现?这是我的模式:
{
"title": "Acc schema",
"type": "object",
"definitions": {
"account": {
"properties": {
"accountId": {
"type": "string",
"minLength": 1,
"maxLength": 15
},
"currency": {
"type": "string",
"minLength": 3,
"maxLength": 3
},
"accountType": {
"type": "string",
"enum": [
”Account”
]
}
},
"required": ["accountId"],
"additionalProperties": false
},
"data": {
"type": "object",
"properties": {
"accounts": {
"type": "array",
"items": {
"anyOf": [
{
"$ref": "#/definitions/account"
}
],
"additionalItems": false
}
},
"additionalProperties": false
}
}
},
"properties": {
"data": {
"$ref": "#/definitions/data"
},
"headers": {
"type": "object"
},
"config": {
"type": "object"
},
"request": {
"type": "object"
},
"status": {
"type": "number"
},
"statusText": {
"type": "string"
}
},
"additionalProperties": false
}
答案
的列表主要关键字为not
和contains
。
您在data
中的definitions
条目可能看起来像这样(也没有不必要的anyOf
):
"data": {
"type": "object",
"properties": {
"accounts": {
"type": "array",
"items": {
"$ref": "#/definitions/account"
},
“not”: {
“contains”: {
“type”: “object”,
“properties”: {
“accountId”: {
“const”: “3”
}
}
}
}
}
}
"additionalProperties": false
}
以上是关于Json-schema-如何验证特定元素不在数组中?的主要内容,如果未能解决你的问题,请参考以下文章
javascript jest中的json-schema验证