我有一个Json模式模板,其中包含对象数组。我需要使用该模板验证Json输入。但我希望这不应该依赖于数组中对象的顺序。
下面我们在模板中有3个不同对象的数组,即abs,端点和分派。我想从这里删除对顺序的依赖。我可以在Json输入模式中提供项目的顺序。它不应该依赖于模板。我使用'ajv‘node js模板来验证带有模板数据的Json输入。任何帮助都将不胜感激。谢谢。
Attached template and input json.
Json Template
var schema1 = {
"additionalProperties" : {
"type" : "integer"
},
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"services": {
"type": "array",
"items": [
{
"type": "object",
"properties": {
"type": { "type": "string", "const" :"abs" },
"id": { "type": "string" },
"name": { "type": "string" },
"appId": { "type": "string" },
"endpoint": { "type": "string" }
},
//"additionalProperties": false
"required": [ "type","id", "name","appId", "endpoint"]
},
{
"type": "object",
"properties": {
"type": { "type": "string", "const" :"endpoint" },
"id": { "type": "string" },
"name": { "type": "string" },
"appPassword": { "type": "string" },
"appId": { "type": "string" },
"endpoint": { "type": "string" }
},
//"additionalProperties": false,
"required": [ "type","id", "name","appPassword","appId", "endpoint"]
}
,
{
"type": "object",
"properties": {
"type": { "type": "string", "const" :"dispatch" },
"serviceIds" : {"type":"array", "items": [{ "type": "string" }]},
"name": { "type": "string" },
"appId": { "type": "string" },
"authoringKey": { "type": "string" },
"version": { "type": "string" },
"region": { "type": "string" },
"id": { "type": "string" }
},
//"additionalProperties": false
"required": [ "type","serviceIds", "name","appId","authoringKey","version","region", "id"]
}
]
}
}
};
Input Json :
{
"name": "ScorpioBot-development",
"description": "",
"services": [
{
"type": "endpoint",
"id": "1",
"name": "development",
"appId": "test",
"appPassword": "test",
"endpoint": "http://localhost:3978"
},
{
"type": "abs",
"id": "49",
"name": "test-development",
"appId": "12323",
"endpoint": "http://localhost:3978/"
},
{
"type": "endpoint",
"id": "11",
"name": "development",
"appId": "test1",
"appPassword": "test1",
"endpoint": "http://localhost:3978"
},
{
"type": "dispatch",
"serviceIds": [
"general"
],
"name": "test_Dispatch",
"appId": "test",
"authoringKey": "1234,
"version": "Dispatch",
"region": "test",
"id": "dispatch"
}
]
}` {
"name": "ScorpioBot-development",
"description": "",
"services": [
{
"type": "endpoint",
"id": "1",
"name": "development",
"appId": "test",
"appPassword": "test",
"endpoint": "http://localhost:3978"
},
{
"type": "abs",
"id": "49",
"name": "test-development",
"appId": "12323",
"endpoint": "http://localhost:3978/"
},
{
"type": "endpoint",
"id": "11",
"name": "development",
"appId": "test1",
"appPassword": "test1",
"endpoint": "http://localhost:3978"
},
{
"type": "dispatch",
"serviceIds": [
"general"
],
"name": "test_Dispatch",
"appId": "test",
"authoringKey": "1234,
"version": "Dispatch",
"region": "test",
"id": "dispatch"
}
]
}`发布于 2018-10-11 18:44:59
items关键字可以采用单个架构,也可以采用数组或架构。
如果提供模式数组,则必须将第一个模式应用于数组中的第一项,然后将第二个模式应用于数组中的第二项,依此类推,以获取n个模式和项。
要更改您的模式,使items的值成为单个模式,请将您的模式数组包装在oneOf中。这意味着数组中的每一项对于oneOf数组值中的一个模式都是有效的。
https://stackoverflow.com/questions/52713839
复制相似问题