Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.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
JSON模式-仅允许现有键作为另一个对象中的值_Json_Jsonschema - Fatal编程技术网

JSON模式-仅允许现有键作为另一个对象中的值

JSON模式-仅允许现有键作为另一个对象中的值,json,jsonschema,Json,Jsonschema,假设我有以下JSON对象: { "firstKey": { "innerKey": null }, "secondKey": { "innerKey": null }, "thirdKey": { "innerKey": "firstKey" } } 通过使用JSON模式,我如何确保innerKey只能是firstKey或secondKey或null(即,我如何只允许来自其他对象的现有键作为值)?正

假设我有以下JSON对象:

{
    "firstKey": {
        "innerKey": null
    },
    "secondKey": {
        "innerKey": null
    },
    "thirdKey": {
        "innerKey": "firstKey"
    }
}

通过使用JSON模式,我如何确保
innerKey
只能是
firstKey
secondKey
null
(即,我如何只允许来自其他对象的现有键作为值)?

正如@Clemens所说的那样,这通常是不可能的。但是,如果您的JSON输入具有
有限的
数量以及定义良好的
属性,那么您可以尝试这样的方法

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "firstKey": {
      "properties": {
        "innerKey": {
          "allOf": [
            {
              "$ref": "#/definitions/innerKey"
            },
            {
              "not": {
                "const": "firstKey"
              }
            }
          ]
        }
      }
    },
    "secondKey": {
      "properties": {
        "innerKey": {
          "allOf": [
            {
              "$ref": "#/definitions/innerKey"
            },
            {
              "not": {
                "const": "secondKey"
              }
            }
          ]
        }
      }
    },
    "thirdKey": {
      "properties": {
        "innerKey": {
          "allOf": [
            {
              "$ref": "#/definitions/innerKey"
            },
            {
              "not": {
                "const": "thirdKey"
              }
            }
          ]
        }
      }
    }
  },
  "definitions": {
    "innerKey": {
      "oneOf": [
        {
          "enum": [
            "firstKey",
            "secondKey",
            "thirdKey"
          ]
        },
        {
          "type": "null"
        }
      ]
    }
  }
}

正如@Clemens所说,这通常是不可能的。但是,如果您的JSON输入具有
有限的
数量以及定义良好的
属性,那么您可以尝试这样的方法

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "firstKey": {
      "properties": {
        "innerKey": {
          "allOf": [
            {
              "$ref": "#/definitions/innerKey"
            },
            {
              "not": {
                "const": "firstKey"
              }
            }
          ]
        }
      }
    },
    "secondKey": {
      "properties": {
        "innerKey": {
          "allOf": [
            {
              "$ref": "#/definitions/innerKey"
            },
            {
              "not": {
                "const": "secondKey"
              }
            }
          ]
        }
      }
    },
    "thirdKey": {
      "properties": {
        "innerKey": {
          "allOf": [
            {
              "$ref": "#/definitions/innerKey"
            },
            {
              "not": {
                "const": "thirdKey"
              }
            }
          ]
        }
      }
    }
  },
  "definitions": {
    "innerKey": {
      "oneOf": [
        {
          "enum": [
            "firstKey",
            "secondKey",
            "thirdKey"
          ]
        },
        {
          "type": "null"
        }
      ]
    }
  }
}

对于当前版本的JSON模式,这是不可能的。您需要在一个单独的验证步骤中进行检查。这可能不是您想要实现的,但您可以定义一个枚举,因此innerKey只能是[“firsKey”、“secondKey”、null]中的一个。但是这个枚举应该通过指定键来构建,这在当前版本的JSON模式中是不可能的。您需要在一个单独的验证步骤中进行检查。这可能不是您想要实现的,但您可以定义一个枚举,因此innerKey只能是[“firsKey”、“secondKey”、null]中的一个。但是这个枚举应该通过指定键来构建。