简体   繁体   English

需要帮助:Json Schema Design

[英]Need help :Json Schema Design

I am designing the the json scheme. 我正在设计json方案。 and I am facing some issues while designing the schema. 在设计架构时我遇到了一些问题。

Here is the problem. 这是问题所在。

I have an array of group objects. 我有一组对象。 and I want this array should contain unique group objects. 我希望此数组应包含唯一的组对象。 I want to make them unique based on object id ( ex group.id) 我想根据对象ID(ex group.id)使它们唯一

The groups array is not unique if (groups[0].id == groups[1].id) , I want to make unique only based on group id, Below is my Json structure. groups数组不是唯一的,如果(groups[0].id == groups[1].id) ,我想仅基于组id来使唯一,下面是我的Json结构。

"groups": {
        "type": "array",
        "items": {"$ref": "#/group"},
        "uniqueItems":true
    },

 "group": {
        "type": "object",
        "properties": {
            "id": {"type": "integer"},
            "type": {
                "type": "string",
                "enum": [
                    "a",
                    "b"
                ]
            },
            "command": {
                "type": "string",
                "enum": [
                    "add",
                    "modify"
                ]
            }
        }
    },

Well, there is no magic bullet here. 好吧,这里没有魔术子弹。 Remind Json-Schema is intended for defining structure of Json Data (not values). 提醒Json-Schema用于定义Json Data(而非值)的结构。

One option would be not consider your groups node an "array" but instead an "object", and use additionalProperties to express that all additional properties should contain "type" and "command" properties. 一种选择不是将您的组节点视为“数组”,而是将其视为“对象”,并使用AdditionalProperties表示所有其他属性都应包含“ type”和“ command”属性。

Then you would use the name of each property in groups as id, so it would be unique. 然后,您将组中每个属性的名称用作id,因此它将是唯一的。

The problem with this approach is that you do not restrict this id to be numeric (It might not be acceptable in your context). 这种方法的问题是您不将这个ID限制为数字(在您的上下文中可能不可接受)。 Even you could use patternProperties to match the "type,command" schema just to numeric "id's". 甚至您也可以使用patternProperties将“类型,命令”模式与数字“ id”进行匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM