枚举列表作为 json 架构中的对象 属性

enum list as object property in json schema

我正在寻找一种方法来声明在枚举列表中定义的对象。

这是我要检查的内容:

{
  "object1": {
    "subobject1": {
      "value": 123
    },
    "subobject2": {
      "value": 456
    }
  },
  "object2": {
    "subobject3": {
      "value": 789
    }
  },
  "object3": {
    "subobject4": {
      "value": 123
    }
  },
  "object4": {
    "subobject5": {
      "value": 234
    }
  }
}

这是我要用于验证的模式:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "definitions": {
        "list1": {
            "enum": [
                "object1",
                "object2",
                "object3",
                "object4"
            ],
       "list2": {
            "enum": [
                "subobject1",
                "subobject2",
                "subobject3",
                "subobject4",
                "subobject5"
            ]
        }

        }
    },
    "properties": {
        "type": {
            "anyOf": [
                {
                    "$ref": "#/definitions/list1"
                }
            ]
        }
    },
    "additionalProperties": false
}

但我收到以下错误:

属性 'object1' 尚未定义,架构不允许附加属性。

我真的想要非常严格,并且只能声明枚举中列出的那个,因为我知道如果我删除 "additionalProperties": false 我可以添加我想要的任何属性并且它有效.

您作为示例给出的实例可以使用以下架构进行验证

{
    "type": "object",
    "definitions": {
      "list1": {
        "properties": {
          "subobject1": {"type": "object"},
          "subobject2": {"type": "object"},
          "subobject3": {"type": "object"},
          "subobject4": {"type": "object"},
          "subobject5": {"type": "object"}
        }
      }
   },
   "properties": {
     "object1": {"type": "object", "$ref": "#/definitions/list1"},
     "object2": {"type": "object", "$ref": "#/definitions/list1"},
     "object3": {"type": "object", "$ref": "#/definitions/list1"},
     "object4": {"type": "object", "$ref": "#/definitions/list1"}
   },
   "additionalProperties": false
}

也许这不是您想要的?我接近了吗?