jsonlint架构未验证

时间:2015-11-08 23:08:00

标签: json node.js npm jsonlint

我想了解一下json。我用npm安装了jsonlint。我完全从this网站复制了一个架构和一个文件。它们如下:

test.json:

[
    {
        "id": 2,
        "name": "An ice sculpture",
        "price": 12.50,
        "tags": ["cold", "ice"],
        "dimensions": {
            "length": 7.0,
            "width": 12.0,
            "height": 9.5
        },
        "warehouseLocation": {
            "latitude": -78.75,
            "longitude": 20.4
        }
    },
    {
        "id": 3,
        "name": "A blue mouse",
        "price": 25.50,
        "dimensions": {
            "length": 3.1,
            "width": 1.0,
            "height": 1.0
        },
        "warehouseLocation": {
            "latitude": 54.4,
            "longitude": -32.7
        }
    }
]

schema.json:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Product set",
    "type": "array",
    "items": {
        "title": "Product",
        "type": "object",
        "properties": {
            "id": {
                "description": "The unique identifier for a product",
                "type": "number"
            },
            "name": {
                "type": "string"
            },
            "price": {
                "type": "number",
                "minimum": 0,
                "exclusiveMinimum": true
            },
            "tags": {
                "type": "array",
                "items": {
                    "type": "string"
                },
                "minItems": 1,
                "uniqueItems": true
            },
            "dimensions": {
                "type": "object",
                "properties": {
                    "length": {"type": "number"},
                    "width": {"type": "number"},
                    "height": {"type": "number"}
                },
                "required": ["length", "width", "height"]
            },
            "warehouseLocation": {
                "description": "Coordinates of the warehouse with the product",
                "$ref": "http://json-schema.org/geo"
            }
        },
        "required": ["id", "name", "price"]
    }
}

我将这两个文件保存在同一目录中。我输入了以下命令。

jsonlint test.json --validate schema.json

并收到以下输出:

验证错误:

Instance is not a required type
uri: urn:uuid:67449791-6ef0-4a5f-8ee1-d9ae1c806249#/items
schemaUri: http://json-schema.org/draft-03/hyper-schema#/properties/items
attribute: type
details: ["http://json-schema.org/draft-03/hyper-schema#","array"]

当我在this网站上的验证器中输入完全相同的代码时,它已检出有效。

当我故意通过删除id(这是必需的)来破坏我的json文件时,我收到了完全相同的输出。

为什么会发生这种情况?我该如何解决?

2 个答案:

答案 0 :(得分:1)

它似乎是一个问题,它有一个依赖(JSV)没有引用该草案04的版本

答案 1 :(得分:1)

jsonlint目前无法针对draft-04架构进行验证。您必须使用draft-03

创建架构

请参阅此处查看针对draft-04验证的工具:http://json-schema.org/implementations.html