【问题标题】:Need help :Json Schema Design需要帮助:Json Schema Design
【发布时间】:2013-05-10 04:56:53
【问题描述】:

我正在设计 json 方案。我在设计架构时遇到了一些问题。

这就是问题所在。

我有一组组对象。我希望这个数组应该包含唯一的组对象。我想根据对象 ID(ex group.id)使它们独一无二

如果 (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"
                ]
            }
        }
    },

【问题讨论】:

  • 请提供一些您尝试过的示例代码,以便其他人可以帮助您

标签: json jsonschema


【解决方案1】:

嗯,这里没有灵丹妙药。 Remind Json-Schema 用于定义 Json 数据的结构(不是值)。

一种选择是不将您的组节点视为“数组”,而是将其视为“对象”,并使用附加属性来表示所有附加属性应包含“类型”和“命令”属性。

然后您将使用组中每个属性的名称作为 id,因此它是唯一的。

这种方法的问题在于您没有将此 id 限制为数字(在您的上下文中可能不可接受)。甚至您也可以使用 patternProperties 将“type,command”模式与数字“id”相匹配。

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多