我如何通知Json模式它的数组中必须至少有一种类型的对象,但其他类型是可选的
我正在为工作更新JSON模式 对于json数组,我们有我如何通知Json模式它的数组中必须至少有一种类型的对象,但其他类型是可选的,json,jsonschema,json-schema-validator,Json,Jsonschema,Json Schema Validator,我正在为工作更新JSON模式 对于json数组,我们有 "accountsInfo": [{ "type":"ADMIN", "firstName":"Bill", "lastName":"Cipher", "emailAddress":"bcipher@gfalls.com" },
"accountsInfo": [{
"type":"ADMIN",
"firstName":"Bill",
"lastName":"Cipher",
"emailAddress":"bcipher@gfalls.com"
}, {
"type":"USER"
"firstName":"Bugs",
"lastName":"Bunny",
"emailAddress":"whats@updoc.org"
}]
此架构的用户类型是可选的,阵列中至少需要1个管理员类型。我该怎么做
下面是模式文件的一部分。它使用的是Json模式7
"accountsInfo": {
"type": "array",
"uniqueItems": true,
"minItems": 2,
"items": [
{
"type": "object",
"required": [
"type",
"firstName",
"lastName",
"emailAddress"
],
"properties": {
"type": {
"type": "string",
"enum": [
"ADMIN",
"USER"
]
},
"firstName": {
"type": "string",
"$ref": "#/definitions/non-empty-string"
},
"lastName": {
"type": "string",
"$ref": "#/definitions/non-empty-string"
},
"emailAddress": {
"type": "string",
"format": "email"
}
}
}
]
}
您可以为此使用“contains”关键字。在伪代码中:“数组必须包含(至少一个)根据此架构成功计算的项”
作为“type”:“object”
和“items”的同级关键字:{…}
,添加:
"contains": {
"properties": {
"type": {
"const": "ADMIN"
}
}
}
另外,您的“items”
关键字中有一个错误:如果您希望该子模式匹配所有项目,而不仅仅是第一个项目,请删除模式周围的额外数组。“items”的数组形式依次将数据中的每个项与模式中的每个项相匹配,并且您只为第一个项指定一个模式,因此第一个项之后的所有项都可以是任何内容
“项”:{..schema..}
不是“项”:[{..schema..}]