Visual Studio代码中的模式验证一次只适用于第一个数组项

问题描述 投票:1回答:1

我对MS Visual Studio Code(Ver.1.3.1.1)的内置JSON模式验证有一个奇怪的问题,我在这里试图将其作为一个最小,完整和可验证的示例。

因此,对于此示例,假设我们有一个名为myjson.json的文件,该文件应根据模式文件myschema.json进行验证(下面包含完整的文件内容)。

在这个星座中,我希望如果你将鼠标指针直接移动到VSCode编辑器的项目(或项目值)上,将显示模式文件中的相应描述文本。

相反,这仅适用于foodItems中的第一项(图1)。所有其他项目均未显示任何内容。(图2)。它看起来甚至根本没有被编辑器处理。

我在这里做错了什么,或者这是VSCode中的一个缺陷?

图1 - 鼠标悬停和验证工作的第一个项目Mouse hover and validation working for the first item

图2 - 鼠标悬停和验证不适用于所有后续项目Mouse hover and validation not working for all subsequent items


文件内容:


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        
        }
    ] 
}

json visual-studio-code jsonschema
1个回答
2
投票

看起来你犯了一个很容易错过的错误。

items可以是JSON模式对象的数组,也可以是JSON模式对象。

如果它是一个对象,则适用数组中的所有项必须与子模式匹配(这是您想要的)。

如果它是一个数组,它会将数组中的模式应用于相同索引处的适用数组中的对象(这就是您所拥有的)。

如果“items”是一个模式,如果所有元素都是验证成功 数组成功验证该架构。

如果“items”是模式数组,则验证成功 实例的元素在同一个模式下验证 位置,如果有的话。

https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4.1

实质上,从items属性值中删除模式周围的方括号。

不过,我还没有检查过你的架构是否存在其他问题。

© www.soinside.com 2019 - 2024. All rights reserved.