Need help :Json Schema Design

馋奶兔 提交于 2020-01-06 18:03:49

问题


I am designing the the json scheme. 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)

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": {
        "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"
                ]
            }
        }
    },

回答1:


Well, there is no magic bullet here. Remind Json-Schema is intended for defining structure of Json Data (not values).

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.

Then you would use the name of each property in groups as id, so it would be unique.

The problem with this approach is that you do not restrict this id to be numeric (It might not be acceptable in your context). Even you could use patternProperties to match the "type,command" schema just to numeric "id's".



来源:https://stackoverflow.com/questions/16475427/need-help-json-schema-design

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!