Visual Studio 代码中的架构验证一次仅适用于第一个数组项
Schema validation in Visual Studio Code only works for the first array item at a time
我对 MS Visual Studio 代码(版本 1.31.1)的内置 JSON 模式验证有一个奇怪的问题,我我试图在此处说明为 最小、完整 和 可验证 示例。
所以对于这个例子,假设我们有一个名为 myjson.json 的文件,该文件应该根据架构文件 [=62= 进行验证](下面包含完整文件内容)。
在这个星座中,我希望如果您将鼠标指针直接移动到项目(或项目值)上 n VSCode 的编辑器,将显示模式文件中的相应描述文本。
相反,这仅适用于 foodItems 中的第一项(图 1)。所有其他项目均不显示任何内容。(图 2)。
看起来编辑器根本没有处理其他项目。
我是不是做错了什么,或者这是 VSCode 的缺陷?
图片 1 - 鼠标悬停和第一项验证工作
图 2 - 鼠标悬停和验证不适用于所有后续项目
文件内容:
myschema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "My Schema",
"type": "object",
"properties": {
"foodItems": {
"type": "array",
"items": [ {"$ref": "#/definitions/foodItem"} ]
}
},
"definitions": {
"foodItemApple": {
"type": "object",
"properties": {
"name": {
"const": "Apple"
},
"amount": {
"type": "number",
"description": "The current amount of apples"
}
},
"required": ["name", "amount"],
"additionalProperties": false
},
"foodItemOrange": {
"type": "object",
"properties": {
"name": {
"const": "Orange"
},
"amount": {
"type": "number",
"description": "The current amount of oranges"
}
},
"required": ["name", "amount"
],
"additionalProperties": false
},
"foodItemCherry": {
"type": "object",
"properties": {
"name": {
"const": "Cherry"
},
"amount": {
"type": "number",
"description": "The current amount of cherries"
}
},
"required": ["name", "amount"],
"additionalProperties": false
},
"foodItem": {
"anyOf": [
{"$ref": "#/definitions/foodItemApple"},
{"$ref": "#/definitions/foodItemOrange"},
{"$ref": "#/definitions/foodItemCherry"}
]
}
}
}
myjson.json
{
"$schema": "./myschema.json" ,
"foodItems": [
{
"name": "Apple",
"amount": 0
},
{
"name": "Orange",
"amount": 0
},
{
"name": "Cherry",
"amount": 0
}
]
}
看起来你犯了一个很容易被忽略的错误。
items
可以是 JSON 模式对象的数组,或 JSON 模式对象。
如果它是一个对象,则适用数组中的所有项目都必须匹配子模式(这就是您想要的)。
如果它是一个数组,它会将数组中的模式应用到适用数组中同一索引处的对象(这是您所拥有的)。
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.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.1
基本上,从 items
属性 值中删除架构周围的方括号。
不过,我还没有检查您的架构是否存在其他问题。
我对 MS Visual Studio 代码(版本 1.31.1)的内置 JSON 模式验证有一个奇怪的问题,我我试图在此处说明为 最小、完整 和 可验证 示例。
所以对于这个例子,假设我们有一个名为 myjson.json 的文件,该文件应该根据架构文件 [=62= 进行验证](下面包含完整文件内容)。
在这个星座中,我希望如果您将鼠标指针直接移动到项目(或项目值)上 n VSCode 的编辑器,将显示模式文件中的相应描述文本。
相反,这仅适用于 foodItems 中的第一项(图 1)。所有其他项目均不显示任何内容。(图 2)。 看起来编辑器根本没有处理其他项目。
我是不是做错了什么,或者这是 VSCode 的缺陷?
图片 1 - 鼠标悬停和第一项验证工作
图 2 - 鼠标悬停和验证不适用于所有后续项目
文件内容:
myschema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "My Schema",
"type": "object",
"properties": {
"foodItems": {
"type": "array",
"items": [ {"$ref": "#/definitions/foodItem"} ]
}
},
"definitions": {
"foodItemApple": {
"type": "object",
"properties": {
"name": {
"const": "Apple"
},
"amount": {
"type": "number",
"description": "The current amount of apples"
}
},
"required": ["name", "amount"],
"additionalProperties": false
},
"foodItemOrange": {
"type": "object",
"properties": {
"name": {
"const": "Orange"
},
"amount": {
"type": "number",
"description": "The current amount of oranges"
}
},
"required": ["name", "amount"
],
"additionalProperties": false
},
"foodItemCherry": {
"type": "object",
"properties": {
"name": {
"const": "Cherry"
},
"amount": {
"type": "number",
"description": "The current amount of cherries"
}
},
"required": ["name", "amount"],
"additionalProperties": false
},
"foodItem": {
"anyOf": [
{"$ref": "#/definitions/foodItemApple"},
{"$ref": "#/definitions/foodItemOrange"},
{"$ref": "#/definitions/foodItemCherry"}
]
}
}
}
myjson.json
{
"$schema": "./myschema.json" ,
"foodItems": [
{
"name": "Apple",
"amount": 0
},
{
"name": "Orange",
"amount": 0
},
{
"name": "Cherry",
"amount": 0
}
]
}
看起来你犯了一个很容易被忽略的错误。
items
可以是 JSON 模式对象的数组,或 JSON 模式对象。
如果它是一个对象,则适用数组中的所有项目都必须匹配子模式(这就是您想要的)。
如果它是一个数组,它会将数组中的模式应用到适用数组中同一索引处的对象(这是您所拥有的)。
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.
https://datatracker.ietf.org/doc/html/draft-handrews-json-schema-validation-01#section-6.4.1
基本上,从 items
属性 值中删除架构周围的方括号。
不过,我还没有检查您的架构是否存在其他问题。