JSONSchema - 必需的属性不起作用

JSONSchema - required properties not working

我是 JSON 的新手,如果我错过了一些非常简单的事情,我不会感到惊讶,但是我尝试并未能找到我在我的架构中到底做错了什么以及为什么它验证了一些事情不正确。 这是我的架构:

apartment_schema = {
    "type": "object",
    "properties": {
        "Apartments": {"type": "object"},
        "properties": {"ap1": {"type": "object",
                                 "required": ["count", "ages"],
                                 "properties": {"count": {"type": "number"},
                                                "ages": {"type": "array", "items": {"type": "number"}}},
                                 "additionalProperties": False,
                                 },
                       "ap2": {"type": "object",
                                "required": ["count", "ages"],
                                "properties": {"count": {"type": "number"},
                                               "ages": {"type": "array", "items": {"type": "number"}}},
                                "additionalProperties": False,
                                },
                       "ap3": {"type": "object",
                                     "required": ["count", "ages"],
                                     "properties": {"count": {"type": "number"},
                                                    "ages": {"type": "array", "items": {"type": "number"}}},
                                     "additionalProperties": False,
                                     },
                       },
        "required": ["ap1", "ap2", "ap3"], 
        "additionalProperties": False,
            },
    "additionalProperties": False,
    "required": ["Apartments"]
}

我正在尝试使用 json.loads 验证字符串,然后针对此模式使用验证函数,但是当我尝试这样做时,我收到此消息:

jsonschema.exceptions.SchemaError: ['ap1', 'ap2', 'ap3'] is not of type 'object', 'boolean'

这是我尝试验证它的方式,并针对什么:

def validateJson(jsonData):
    try:
        jsonschema.validate(instance=jsonData, schema=apartment_schema)
    except jsonschema.exceptions.ValidationError:
        return False
    return True
print(validateJson(json.loads("{\"Apartments\": {\"ap1\": {\"count\": 1, \"ages\": [40]},\"ap3\": {\"ages\": [10,15]}}}"))

此验证通过,如果我只从架构中删除一个必需的部分,即使它不应该通过,我也不会收到错误消息,因为它缺少一个必需的参数(计数).当我输入不同的字符串时,其他“必填”字段的 none 似乎也在工作,即使它们没有引发错误。 我在这里做错了什么?

您在“公寓”属性声明的正下方有一个额外的 properties 关键字,该关键字不应存在 - 因此该关键字下方的所有内容都在错误的级别进行了解析。我想您是希望属性“ap1”、“ap2”和“ap3”在数据中与“公寓”处于同一级别?