json schema - 父数组(对象数组)项具有给定值的内部数组(基元数组)

时间:2018-05-08 22:06:33

标签: arrays json jsonschema json-schema-validator

请随时更新此问题的拟合标题。

我正在努力实现this之类的东西。输入JSON / data JSON必须具有OWNER和CHARGE_TO帐户,但这些帐户不必位于同一帐户(即一个帐户可以是OWNER,另一个可以是CHARGE_TO),并且不得包含任何其他角色的帐户。

注意:需要定义易于维护的JSON架构。即应该很容易在条件中添加新角色。我的有效和无效JSON是,

*************************** VALID JSON1 *********************************
{
  "user": [
    {
      "name": "user1",
      "roles": ["OWNER"]
    },
    {
      "name": "user2",
      "roles": ["ANY_OTHER_ROLE"]
    },
    {
      "name": "user3",
      "roles": ["CHARGE_TO"]
    }]  
}
*************************** VALID JSON2 *********************************
{
  "user": [
    {
      "name": "user1",
      "roles": ["OWNER", "CHARGE_TO"]
    },
    {
      "name": "user2",
      "roles": ["ANY_OTHER_ROLE"]
    }]  
}
*************************** INVALID JSON *********************************
{
  "user": [
    {
      "name": "user1",
      "roles": ["OWNER"]
    },
    {
      "name": "user2",
      "roles": ["ANY_OTHER_ROLE"]
    }]  
}

如果JSON的用户具有角色(“OWNER”&“CHARGE_TO”)或者具有角色的用户( user1 - “OWNER”,user3-“CHARGE_TO”,则JSON有效“,其他具有任何其他角色的用户)。

下面是我尝试的JSON模式(draft_07)。 这不是一个工作模式

{
"$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Complex inner array",
  "type": "object",
  "properties": {
    "user": {
      "type": "array",
      "contains": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "orderRoles": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "string" }
          }
        },
        "oneOf": [
          { "properties": { "roles": { "enum": ["OWNER", "CHARGE_TO"] }}},
          { "properties": { "roles": { "enum": ["OWNER"] }}},
          { "properties": { "roles": { "enum": ["CHARGE_TO"] }}}
        ]
      },
      "items": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "orderRoles": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "string" }
          }
        }
      }
    }
  }
}

2 个答案:

答案 0 :(得分:0)

以下是我的工作架构。这不是一个优雅的解决方案,因为如果添加新角色(" ADMIN"),即架构条件为An input JSON/data JSON must have an OWNER, ADMIN and a CHARGE_TO Account, but these don't have to be on the same Account (I.e. one Account can be OWNER, one Account can be ADMIN & another can be CHARGE_TO) and must not contain account with any other roles,架构会变得庞大。请随时改进这一点。感谢。

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Complex inner array",
  "type": "object",
  "allOf": [
    {
      "properties": {
        "account": {
          "type": "array",
          "contains": {
            "type": "object",
            "properties": {
              "roles": {
                "type": "array",
                "minItems": 1,
                "contains": { "enum": ["OWNER"] }
              }
            }
          }
        }
      }
    },
    {
      "properties": {
        "account": {
          "type": "array",
          "contains": {
            "type": "object",
            "properties": {
              "roles": {
                "type": "array",
                "minItems": 1,
                "contains": { "enum": ["CHARGE_TO"] }
              }
            }
          }
        }
      }
    },
    {
      "properties": {
        "account": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "roles": {
                "type": "array",
                "minItems": 1,
                "items":{
                  "enum": ["CHARGE_TO", "OWNER"]
                }
              }
            }
          }
        }
      }
    }
  ]
}

答案 1 :(得分:0)

您需要隔离检查特定角色的模式部分。然后你可以将它们与allOf结合起来。尽可能少的重复,您可以轻松添加另一个约束。

{
  "type": "object",
  "properties": {
    "user": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "name": { "type": "string" },
          "roles": {
            "type": "array",
            "items": { "type": "string" }
          }
        }
      },
      "allOf": [
        { "$ref": "#/definitions/require-role-owner" },
        { "$ref": "#/definitions/require-role-charge-to" }
      ]
    }
  },
  "definitions": {
    "require-role-owner": {
      "contains": {
        "properties": {
          "roles": {
            "contains": { "const": "OWNER" },
            "allOf": [{ "$ref": "#/definitions/not-other-role" }]
          }
        }
      }
    },
    "require-role-charge-to": {
      "contains": {
        "properties": {
          "roles": {
            "contains": { "const": "CHARGE_TO" },
            "allOf": [{ "$ref": "#/definitions/not-other-role" }]
          }
        }
      }
    },
    "not-other-role": {
      "items": { "enum": ["OWNER", "CHARGE_TO"] }
    }
  }
}
相关问题