项目数组的json模式(在前面的模式中引用)。

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

我正试图为一个json文档设计一个模式,这个模式在顶层是一个项目数组。每个项目都描述了一个 "git repo",我们有一些映射。为什么会失败?

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "http://i.am.awesome.com",
    "title": "title of the schema for our projects",
    "description": "description of the schema for our projects",
    "definitions": {
        "proj": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
                "name": {
                    "type": "string"
                },
                "visibility": {
                    "type": "string",
                    "enum": [
                        "private",
                        "public"
                    ]
                },
                "languages": {
                    "type": "array",
                    "minItems": 2,
                }
            },
            "required": [
                "name",
                "visibility",
                "languages",
            ]
        }
    },
    "type": "array",
    "items": {
        "type": {
            "$ref": "#/definitions/proj"
        }
    }
}

我使用python 3.8和jsonschema,得到以下错误信息

Failed validating 'anyOf' in metaschema['properties']['items']:
    {'anyOf': [{'$ref': '#'}, {'$ref': '#/definitions/schemaArray'}],
     'default': True}

On schema['items']:
    {'type': {'$ref': '#/definitions/proj'}}

有趣的是,如果我不关心列表,而只检查一个元素,只需让

$ref": "#/definitions/proj

所以我的引用是正确的,只是不知道为什么对于同样的项目的列表不能用。

arrays json jsonschema json-schema-validator
1个回答
2
投票

的"$..."。$ref 应直接包含在 items 关键词,而不是下 items.type. type 是一个保留关键字,只能是一个字符串或数组,而不能是一个对象。这使得你的模式无效。

这将是一个有效的模式(为了可读性,省略了一些细节)。

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "proj": {
            "type": "object"
        }
    },
    "type": "array",
    "items": {
        "$ref": "#/definitions/proj"
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.