我有这样一个JSON Schema文件(缩小了以避免显示与问题无关的东西)。
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "urn:jsonschema:testing:frmwk",
"type": "object",
"title": "The root schema",
"default": {},
"required": [
"processOrder"
],
"additionalProperties": true,
"properties": {
"processOrder": {
"$id": "#processOrder",
"type": "array",
"title": "The processOrder schema",
"default": [],
"additionalItems": true,
"items": {
"anyOf": [
{
"$id": "#0",
"type": "object",
"title": "The first anyOf schema",
"default": {},
"additionalProperties": true,
"properties": {
"cleanHistory": {
"$id": "#cleanHistory",
"type": "object",
"title": "The cleanHistory schema",
"default": {},
"additionalProperties": true,
"properties": {}
}
}
},
{
"$id": "#3",
"type": "object",
"title": "The fourth anyOf schema",
"default": {},
"additionalProperties": true,
"properties": {
"processEvents": {
"$id": "#processEvents",
"type": "object",
"title": "The processEvents schema",
"default": {},
"required": [
"identityTrigger"
],
"additionalProperties": true,
"properties": {
"identityTrigger": {
"$id": "#identityTrigger",
"type": "string",
"title": "The identityTrigger schema",
"default": ""
}
}
}
}
}
],
"$id": "#items"
}
}
}
}
我想验证的JSON是:
{
"description": "description",
"requesteeName": "05300005",
"processOrder": [
{"cleanHistory": {} },
{"processEvents": {
"identityTrigger": "some trigger"
}}
],
"deleteObjects": "true"
}
现在,我希望当我删除这个字符串时,它会失败。
"identityTrigger": "some trigger"
因为 "identityTrigger" 属性在processEvents对象中的所需数组中,但没有失败,一定是数组匹配(processOrder数组)出了问题。但是没有失败,一定是数组匹配(processOrder数组)出了问题。谁能给我一个建议呢,谢谢
验证仍然成功的原因是,第一个 anyOf
选项没有指定任何 required
属性,同时允许任何 additionalProperties
因此,一个空的JSON对象总是对第一个选项有效。anyOf
以避免空对象对其中任何一个有效。
然而,这里似乎还有一些需要澄清的地方。
additionalProperties
关键字默认为 true
而在这种情况下不需要提及。根据 json-schema.org:负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人: 负责人:
additionalProperties
关键字用于控制对额外的东西的处理,也就是其名称未在properties
关键字。默认情况下,允许任何附加属性。
additionalItems
关键字只适用于 items
是一个数组(即数组在特定位置上是一个不同类型的元组)。根据 json-schema.org:时间
items
是一个单一的模式,该additionalItems
关键字是没有意义的,它不应该被使用。
default
的值是空对象,这对描述的结构来说是无效的。required
属性。最好把这些 default
的值也被删除了--除非这只是因为您在这个问题的范围内删除了它们的内容,以保持简单。我已经更好地理解了它是如何工作的,就像 @Carten 在评论中说的那样。任何 单项不需要属性的情况下,无法工作,因为任何属性的项目都会被验证,正确的模式应该是(粗体的变化)。
{
"$schema": "http://json-schema.org/draft-07/schema",
"$id": "urn:jsonschema:testing:frmwk",
"type": "object",
"title": "The root schema",
"default": {},
"required": [
"processOrder"
],
"additionalProperties": true,
"properties": {
"processOrder": {
"$id": "#processOrder",
"type": "array",
"title": "The processOrder schema",
"default": [],
"additionalItems": false,
"items": {
"anyOf": [
{
"$id": "#0",
"type": "object",
"title": "The first anyOf schema",
"default": {},
"additionalProperties": true,
"required": ["cleanHistory"],
"properties": {
"cleanHistory": {
"$id": "#cleanHistory",
"type": "object",
"title": "The cleanHistory schema",
"default": {},
"additionalProperties": true,
"properties": {}
}
}
},
{
"$id": "#3",
"type": "object",
"title": "The fourth anyOf schema",
"default": {},
"additionalProperties": true,
"required": ["processEvents"],
"properties": {
"processEvents": {
"$id": "#processEvents",
"type": "object",
"title": "The processEvents schema",
"default": {},
"required": [
"identityTrigger"
],
"additionalProperties": true,
"properties": {
"identityTrigger": {
"$id": "#identityTrigger",
"type": "string",
"title": "The identityTrigger schema",
"default": ""
}
}
}
}
}
],
"$id": "#items"
}
}
}
}
主要的变化是 2: