AJV架构验证错误
问题描述:
我有输入JSON像下面,AJV架构验证错误
{"contents":[{"type":"field"},{"type":"field","itemId":"594b9980e52b5b0768afc4e8"}]}
的条件是, 如果类型是“场”,然后“ITEMID”应该是必需字段 并且如果类型是“FIELDGROUP”或“款”,然后“ITEMID”是可选的
这是JSON模式我试着和它的工作不正常,
"type": "object",
"additionalProperties": false,
"properties" : {
"contents" : {
"type" : "array",
"items": {"$ref": "#displayItem" }
}
},
"definitions": {
"displayItem" : {
"id": "#displayItem",
"type": "object",
"items": {
"anyOf": [
{"$ref": "#fieldType"},
{"$ref": "#fieldGroupSubSectionType"}
]
}
},
"fieldType" : {
"id": "#fieldType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": ["field"]
}
}
},
"fieldGroupSubSectionType" : {
"id": "#fieldGroupSubSectionType",
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": [ "string", "null" ]
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
}
任何帮助/解决方法w^ith示例Json Schema来实现上述用例,我们感激不尽。
答
如果我理解了你想要的东西的描述,那么你提供的json例子是无效的,因为它有一个类型:“field”但没有“itemId”属性。
假设这是真的。除了使用
类型的: “串”,无效]
使用需要财产。
我改变你的架构了一下,而不必单独的定义我内联它们,但除此之外,(和使用要求的)是一样的:
{
"type": "object",
"additionalProperties": false,
"properties": {
"contents": {
"type": "array",
"items": {
"anyOf": [
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"field"
]
}
},
"required": [
"itemId"
]
},
{
"type": "object",
"additionalProperties": false,
"properties": {
"itemId": {
"type": "string"
},
"type": {
"type": "string",
"enum": [
"fieldGroup",
"subSection"
]
}
}
}
]
}
}
}
}
答
下面是一些清理你的答案获得最佳做法和风格。诀窍是你需要使用暗示“暗示b < =>(不是a)或b”。在这种情况下,你有“type =字段意味着itemId是必需的< =>类型不是字段或itemId是必需的”。
{
"type": "object",
"properties": {
"contents": {
"type": "array",
"items": { "$ref": "#/definitions/displayItem" }
}
},
"definitions": {
"displayItem": {
"type": "object",
"properties": {
"itemId": { "type": "string" },
"type": { "enum": ["field", "fieldGroup", "subSection"] }
},
"anyOf": [
{ "not": { "$ref": "#/definitions/fieldType" } },
{ "required": ["itemId"] }
]
},
"fieldType": {
"properties": {
"type": { "enum": ["field"] }
}
}
}
}
谢谢佩德罗。有效**。 – Ragubathy