json模式,用于项的数组(在该模式的前面引用)



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

{
"$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"
}
}
}

我正在使用带有jsonschema的python 3.8,并得到这个错误

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

有趣的是,如果我不关心列表,并且正在对单个元素进行模式检查,那么只需使用

$ref": "#/definitions/proj

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

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

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

{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"proj": {
"type": "object"
}
},
"type": "array",
"items": {
"$ref": "#/definitions/proj"
}
}

最新更新