【问题标题】:JSON SCHEMA - How can i check a values exists in an arrayJSON SCHEMA - 我如何检查数组中是否存在值
【发布时间】:2017-06-10 19:15:32
【问题描述】:

我有一个简单的问题。我有一个属性 groupBy,它是一个数组,只包含两个可能的值“产品”和“日期”。现在我想根据 groupBy 数组中的 value 存在 来创建另一个所需的属性。在这种情况下,当我的 groupBy 数组包含 "date" 我想要求分辨率!我该怎么做?

谁可以检查数组是否包含值?

var data = {
    "pcsStreamId": 123123,
    "start": moment().valueOf(),
    "end": moment().valueOf(),
    "groupBy" : ["product"]
};

var schema = {
        "type": "object",
        "properties": {
            "pcsStreamId": { "type": "number" },
            "start": { "type": "integer", "minimum" : 0 },
            "end": { "type": "integer", "minimum" : 0 },
            "groupBy": {
                "type": "array",
                "uniqueItems": true,
                "items" : {
                    "type": "string",
                    "enum": ["product", "date"]
                },
               "oneOf": [
                   {
                       "contains": { "enum": ["date"] },
                       "required": ["resolution"]
                   }
                ]
            },
            "resolution" : {
                "type": "string",
                "enum": ["day", "year", "month", "shift"]
            },
        },
        "required": ["pcsStreamId", "start", "end", "groupBy"]

};

【问题讨论】:

    标签: jsonschema


    【解决方案1】:

    为了解决这个问题,我们必须使用一个称为蕴含的布尔逻辑概念。为了将要求放在布尔逻辑术语中,我们会说“groupBy”包含“日期”暗示需要“分辨率”。暗示可以表示为“(不是A)或B”。换句话说,要么“groupBy”不包含“日期”,要么需要“分辨率”。在这种形式下,应该更清楚如何实现解决方案。

    {
      "type": "object",
      "properties": {
        "pcsStreamId": { "type": "number" },
        "start": { "type": "integer", "minimum": 0 },
        "end": { "type": "integer", "minimum": 0 },
        "groupBy": {
          "type": "array",
          "uniqueItems": true,
          "items": { "enum": ["product", "date"] }
        },
        "resolution": { "enum": ["day", "year", "month", "shift"] }
      },
      "required": ["pcsStreamId", "start", "end", "groupBy"],
      "anyOf": [
        { "not": { "$ref": "#/definitions/contains-date" } },
        { "required": ["resolution"] }
      ],
      "definitions": {
        "contains-date": {
          "properties": {
            "groupBy": {
              "contains": { "enum": ["date"] }
            }
          }
        }
      }
    }
    

    编辑

    此答案使用新的 draft-06 contains 关键字。我之所以使用它是因为提问者使用了它,但是如果您在草稿 04 上,则可以改用“包含日期”的这个定义。它使用另一个逻辑标识 (∃x A ¬∀x ¬A) 来获得 contains 关键字的功能。

    {
      "definitions": {
        "contains-date": {
          "properties": {
            "groupBy": {
              "not": {
                "items": {
                  "not": { "enum": ["date"] }
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 我想指出,“contains”关键字仅在 json 模式 draft6 中有效,目前大多数验证器都支持 Draft4。否则很好的解决方案:thumbsup:
    【解决方案2】:

    您的 groupby 属性似乎是一个对象。可能是您正在谈论的嵌套枚举属性(因为它是一个数组并包含日期和产品)。但是,一旦您将此 json 解析为一个对象,您可以检查以下内容:

    groupby.items.enums.indexOf('date') === -1

    array.indexOf(anyItem) 返回项目的索引,如果存在于数组中,则返回 -1。

    希望有帮助

    【讨论】:

    • 也许我写得还不够。我想在 JSON Schema 中执行此操作,而不是使用应用程序逻辑。但是谢谢
    猜你喜欢
    • 2021-01-28
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    • 2011-01-27
    • 1970-01-01
    • 2020-03-22
    • 2016-06-13
    相关资源
    最近更新 更多