JSON Schema - 如果值不正确,则拒绝使用对象

时间:2015-05-01 14:39:44

标签: json

我试图找出当使用BatchChangeOperation并且操作不等于MOVE时如何阻止使用“MoveToArchiveSection”。

base R

当操作等于“MOVE”时,您可以看到我设置了所需的“MoveToArchiveSection”,但是当操作等于“UPDATE”,“EXTRACT”或“DELETE”时,这并不妨碍使用“MoveToArchiveSection”。当操作等于“MOVE”时,应始终需要“MoveToArchiveSection”。

当操作等于MOVE时,我的JSON输入应如下所示:

rUnique <- unique(r)
df2 <- df[df$freq %in% rUnique,]
x <- data.frame(id = NA, freq = rUnique) 

for (i in 1:length(rUnique)) {
    x[i,1] <- sample(df2[df2[, 2] == rUnique[i], 1], 1)
}
print(x)

当Operation equal UPDATE:

时,我的JSON输入应该如下所示
{
    "BatchChangeOperation": {
        "oneOf": [
            { "operation": "DELETE" },
            { "operation": "UPDATE" },
            { "operation": "EXTRACT" },
            { "operation": "MOVE", 
                "required": [ "MoveToArchiveSection" ] 
            }
        ]
    },
    "MoveToArchiveSection": {
        "TargetArchiveSectionId": {
        },
        "required": [
            "TargetArchiveSectionId" 
        ]
    }
}

你的帮助很大!

1 个答案:

答案 0 :(得分:0)

为了得到你想要的东西,你的jsonschema必须看起来像这样(使用MOVE,DELETE,EXTRACT等的枚举......)

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "BatchChangeOperation": {
      "oneOf": [
        {
          "properties": {
            "operation": {
              "enum": [
                "DELETE"
              ]
            }
          }
        },
        {
          "properties": {
            "operation": {
              "enum": [
                "MOVE"
              ]
            },
            "MoveToArchiveSection": {
              "properties": {
                "TargetArchiveSectionId": {
                  "type": "string"
                }
              },
              "required": [
                "TargetArchiveSectionId"
              ]
            }
          },
          "required": [
            "MoveToArchiveSection"
          ]
        }
      ]
    }
  }
}

然后这个json

{
  "BatchChangeOperation": {
    "operation": "MOVE"
  }
}

没有验证,但是

{
  "BatchChangeOperation": {
    "operation": "MOVE",
    "MoveToArchiveSection": {
      "TargetArchiveSectionId": "blabla"
    }
  }
}

确实

希望这有帮助!