JSON 架构仅针对数组的第一个元素报告错误
JSON Schema reporting error only for first element of array
我有以下 JSON 文档。
[
{
"name": "aaaa",
"data": {
"key": "id",
"value": "aaaa"
}
},
{
"name": "bbbb",
"data": {
"key": "id1",
"value": "bbbb"
}
}
]
下面是我为上述内容创建的 JSON 架构。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
"key": {
"type": "string",
"enum": [
"id",
"temp",
]
},
"value": {
"type": "string",
}
},
"required": [
"key",
"value"
]
}
},
"required": [
"name",
"data"
]
}
]
}
根据架构,data.key 的值对于数组中的第二项无效。但任何在线模式验证器都找不到。如果我们在第一个数组元素中使用不同的值,它会抛出异常错误。
我认为我的模式不知何故是错误的。我期望的是,如果数组的任何子项的值不在枚举列表中,则应报告它们。
这是一个很容易犯的错误,所以不要为此自责!
items
可以是数组也可以是对象。如果它是一个数组,它会验证实例数组中该位置的对象。这是 JSON 架构规范(草案 7)
的摘录
The value of "items" MUST be either a valid JSON Schema or an array of
valid JSON Schemas.
If "items" is a schema, validation succeeds if all elements in the
array successfully validate against that schema.
If "items" is an array of schemas, validation succeeds if each element
of the instance validates against the schema at the same position, if
any.
JSON Schema (validation) draft-7 items
删除方括号可为您提供正确的架构...
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items":
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
"key": {
"type": "string",
"enum": [
"id",
"temp",
]
},
"value": {
"type": "string",
}
},
"required": [
"key",
"value"
]
}
},
"required": [
"name",
"data"
]
}
}
我有以下 JSON 文档。
[
{
"name": "aaaa",
"data": {
"key": "id",
"value": "aaaa"
}
},
{
"name": "bbbb",
"data": {
"key": "id1",
"value": "bbbb"
}
}
]
下面是我为上述内容创建的 JSON 架构。
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items": [
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
"key": {
"type": "string",
"enum": [
"id",
"temp",
]
},
"value": {
"type": "string",
}
},
"required": [
"key",
"value"
]
}
},
"required": [
"name",
"data"
]
}
]
}
根据架构,data.key 的值对于数组中的第二项无效。但任何在线模式验证器都找不到。如果我们在第一个数组元素中使用不同的值,它会抛出异常错误。
我认为我的模式不知何故是错误的。我期望的是,如果数组的任何子项的值不在枚举列表中,则应报告它们。
这是一个很容易犯的错误,所以不要为此自责!
items
可以是数组也可以是对象。如果它是一个数组,它会验证实例数组中该位置的对象。这是 JSON 架构规范(草案 7)
The value of "items" MUST be either a valid JSON Schema or an array of valid JSON Schemas.
If "items" is a schema, validation succeeds if all elements in the array successfully validate against that schema.
If "items" is an array of schemas, validation succeeds if each element of the instance validates against the schema at the same position, if any.
JSON Schema (validation) draft-7 items
删除方括号可为您提供正确的架构...
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "array",
"items":
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"data": {
"type": "object",
"properties": {
"key": {
"type": "string",
"enum": [
"id",
"temp",
]
},
"value": {
"type": "string",
}
},
"required": [
"key",
"value"
]
}
},
"required": [
"name",
"data"
]
}
}