JSON Schema Draft 7 在验证数组对象中必需的 属性 时出现问题
JSON Schema Draft 7 issue in validating required property in array object
我有这个 JSON 架构文件(缩小到不显示与问题无关的内容):
{
"$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 数组)一定有问题。
有人可以给我一个建议吗?
谢谢
我已经设法更好地理解它是如何工作的,正如@Carsten 在评论中所说,anyOf 没有必需属性的单个项目不起作用,因为任何项目有任何 属性 会被验证。
正确的架构是(粗体显示更改):
{
"$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 个:
- 在数组对象定义中添加了"additionalItems": false
- 添加 "required": ["cleanHistory"] 和 "required": ["processEvents"] 每个相应的根属性项目,在我的例子中(“cleanHistory”和“processEvents”)这样 AnyOf 将强制使用列出的模式之一来验证数组中的项目。
验证仍然成功的原因是第一个 anyOf
选项在允许任何 additionalProperties
的同时没有指定任何 required
属性,因此空 [=41] =] 对象始终对第一个选项有效。
解决方法是期望每个 anyOf
中至少有一个 属性,以避免空对象对它们中的任何一个都有效。
但是,这里似乎还有一些额外的要点需要澄清:
additionalProperties
关键字默认为 true
,在这种情况下无需提及。根据 json-schema.org:
The additionalProperties
keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties
keyword. By default any additional properties are allowed.
additionalItems
关键字仅在 items
是数组时适用(即数组是特定位置的不同类型的元组)。根据 json-schema.org:
When items
is a single schema, the additionalItems
keyword is meaningless, and it should not be used.
- 您的
default
值是空对象,对于描述的具有 required
属性的结构无效。最好也把那些 default
值也去掉——除非那只是因为你在这个问题的范围内删除了它们的内容以保持简单。
我有这个 JSON 架构文件(缩小到不显示与问题无关的内容):
{
"$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 数组)一定有问题。 有人可以给我一个建议吗? 谢谢
我已经设法更好地理解它是如何工作的,正如@Carsten 在评论中所说,anyOf 没有必需属性的单个项目不起作用,因为任何项目有任何 属性 会被验证。 正确的架构是(粗体显示更改):
{
"$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 个:
- 在数组对象定义中添加了"additionalItems": false
- 添加 "required": ["cleanHistory"] 和 "required": ["processEvents"] 每个相应的根属性项目,在我的例子中(“cleanHistory”和“processEvents”)这样 AnyOf 将强制使用列出的模式之一来验证数组中的项目。
验证仍然成功的原因是第一个 anyOf
选项在允许任何 additionalProperties
的同时没有指定任何 required
属性,因此空 [=41] =] 对象始终对第一个选项有效。
解决方法是期望每个 anyOf
中至少有一个 属性,以避免空对象对它们中的任何一个都有效。
但是,这里似乎还有一些额外的要点需要澄清:
additionalProperties
关键字默认为true
,在这种情况下无需提及。根据 json-schema.org:The
additionalProperties
keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in theproperties
keyword. By default any additional properties are allowed.additionalItems
关键字仅在items
是数组时适用(即数组是特定位置的不同类型的元组)。根据 json-schema.org:When
items
is a single schema, theadditionalItems
keyword is meaningless, and it should not be used.- 您的
default
值是空对象,对于描述的具有required
属性的结构无效。最好也把那些default
值也去掉——除非那只是因为你在这个问题的范围内删除了它们的内容以保持简单。