在JSON模式中将对象属性键用作枚举 [英] Use object property keys as enum in JSON schema
问题描述
我正在尝试使用JSON模式验证JSON文件,以查找断开引用"的情况.本质上,我的文件由项目和组组成,每个项目都属于groups属性键所引用的单个组,如下所示:
I'm trying to validate a JSON file using JSON Schema, in order to find cases of "broken references". Essentially my file consists of items and groups, with each item belonging to a single group referenced by the groups property key, like so:
{
"items": {
"banana": {
"name": "Banana",
"group": "fruits"
},
"apple": {
"name": "Apple",
"group": "fruits"
},
"carrot": {
"name": "Carrot",
"group": "vegetables"
},
"potato": {
"name": "Potato",
"group": "vegetables"
},
"cheese": {
"name": "Cheese",
"group": "dairy"
}
},
"groups": {
"fruits": {
"name": "Fruits"
},
"vegetables": {
"name": "Vegetables"
}
}
}
在上面的示例中,项目cheese
被视为无效,因为groups
对象中没有dairy
属性.我尝试使用以下架构对此进行验证:
In the example above the item cheese
is to be considered invalid, as there are no dairy
property in the groups
object. I've tried to validate this using the following schema:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "Food",
"id": "food",
"type": "object",
"properties": {
"items": {
"type": "object",
"patternProperties": {
"^[A-Za-z0-9-_.:=]+$": {
"properties": {
"name": {
"type": "string",
"pattern": "^[A-Za-z- ]+$"
},
"group": {
"pattern": "^[a-z]+$",
"enum": {
"$data": "/groups"
}
}
}
}
}
},
"groups": {
"type": "object",
"patternProperties": {
"^[A-Za-z0-9-_]+$": {
"properties": {
"name": {
"type": "string",
"pattern": "^[A-Za-z- ]+$"
}
}
}
}
}
},
"additionalProperties": false
}
这具有group
的enum
由groups
中的属性值填充的效果,但是我想做的是使用groups
中定义的属性 keys
This has the effect that the enum
for group
is populated by the property values in groups
, but what I want to do is use the property keys defined in groups
.
如果我添加一个属性,例如groupIds
并将其作为在groups
中找到的所有属性键的数组,并将枚举指定为"$data": "/groupIds"
即可,因此我将其视为JSON指针问题.
If I add a property like e.g. groupIds
and let that be an array of all property keys found in groups
and specify the enum as "$data": "/groupIds"
it does work, so I take this to be a JSON pointer issue.
JSON模式中的enum
关键字定义为:
The enum
keyword in JSON Schema is defined as:
此关键字的值必须为数组.这个数组应该至少有一个元素.数组中的元素应该是唯一的.
The value of this keyword MUST be an array. This array SHOULD have at least one element. Elements in the array SHOULD be unique.
因此,如果我只能获取JSON指针来引用对象的键而不是其值,我想枚举验证就可以了.我在想类似"$data": "/groups/.keys"
,"$data": "/groups/$keys"
之类的东西,但是在谷歌搜索或阅读规范时没有找到它.有这样的事情还是曾经有人提出过?
So if I could only get JSON pointer to reference an object's keys rather than its values I guess the enum validation would just work. I'm thinking something like "$data": "/groups/.keys"
, "$data": "/groups/$keys"
or similar, but haven't found it while googling or reading the spec. Is there such a thing or has it ever been proposed?
推荐答案
没有这样的东西.它非常接近JSON中的通用表达式,并且可能有一些用例,但是没有这样的规范.
There is no such thing. It’s very close to general expressions inside JSON and it may have some use cases, but there is no such specification.
这篇关于在JSON模式中将对象属性键用作枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!