Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays json模式-父数组(对象数组)项具有具有给定值的内部数组(基元数组)_Arrays_Json_Jsonschema_Json Schema Validator - Fatal编程技术网

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

Arrays json模式-父数组(对象数组)项具有具有给定值的内部数组(基元数组),arrays,json,jsonschema,json-schema-validator,Arrays,Json,Jsonschema,Json Schema Validator,请随时更新此问题的配件标题 我正在努力实现这样的目标。输入JSON/data JSON必须有一个所有者和一个收费账户,但它们不必在同一个账户上(即一个账户可以是所有者,另一个账户可以是收费账户),并且不得包含具有任何其他角色的账户 注意:需要定义易于维护的JSON模式。i、 e.应易于在条件中添加新角色。我的有效和无效JSON是 *************************** VALID JSON1 ********************************* { "user"

请随时更新此问题的配件标题

我正在努力实现这样的目标。输入JSON/data JSON必须有一个所有者和一个收费账户,但它们不必在同一个账户上(即一个账户可以是所有者,另一个账户可以是收费账户),并且不得包含具有任何其他角色的账户

注意:需要定义易于维护的JSON模式。i、 e.应易于在条件中添加新角色。我的有效和无效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的用户具有角色(“所有者”和“收费对象”),或者如果用户具有角色(user1-“所有者”,user3-“收费对象”,其他用户具有任何其他角色),则JSON有效

下面是我尝试过的JSON模式(草案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" }
          }
        }
      }
    }
  }
}

下面是我的工作模式。这不是一个优雅的解决方案,因为如果添加了一个新角色(“管理员”),模式将变得巨大,即模式条件将
输入JSON/数据JSON必须有一个所有者、管理员和收费账户,但这些不必在同一个账户上(即一个账户可以是所有者,一个账户可以是管理员,另一个账户可以是收费账户)并且不能包含具有任何其他角色的帐户
。请随时改进。谢谢

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

您需要隔离模式中检查特定角色的部分。然后您可以将它们与
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"] }
    }
  }
}

enum
的使用更改为
const
将为您提供部分帮助。枚举用于特定值,而不是数组中的特定值。我还建议您尝试使用来帮助您修复模式。调试的一个好方法是将子模式替换为
false
,以查看是否对路径进行了计算。如果你在尝试了更多之后仍然有问题,请告诉我,我可能会提供帮助。很难弄清楚你在问什么。与其链接到另一个问题并说“像这样”,不如在这里解释你的要求,解释你的规则,以及你期望发生的事情和没有发生的事情。@Relequestual-我更新了这个问题,以进一步解释我的场景。我希望现在清楚了。谢谢你的回复。在我发布的答案中做了类似的事情。但是你贴的东西不符合我的要求。例如:验证应该失败,因为数组中有一个新角色,但是它通过了<代码>{“用户”:[{“名称”:“用户1”,“角色”:[“所有者”,“收费对象”,“任何其他角色”]}抱歉,我错过了“不得包含任何其他角色的帐户”要求。我更新了答案以考虑到这一点。