简体   繁体   English

JSONSchema - 必需的属性不起作用

[英]JSONSchema - required properties not working

I'm new to JSON and I wouldn't be surprised if I missed something really simple, however I tried and failed to find what exactly I am doing wrong in my schema and why its validating some things incorrectly.我是 JSON 的新手,如果我错过了一些非常简单的事情,我不会感到惊讶,但是我尝试并未能找到我在架构中到底做错了什么以及为什么它错误地验证了某些事情。 This is my schema:这是我的架构:

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

I am trying to validate a string by using json.loads and then the validate function against this schema, but when i try this, I get this message:我正在尝试使用 json.loads 验证字符串,然后针对此架构验证 function,但是当我尝试此操作时,我收到此消息:

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

Here is my how I try to validate it, and against what:这是我尝试验证它的方法,以及针对什么:

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]}}}"))

This validation passes and I don't get the error message if I just remove only the one required part out of the schema, even though it shouldn't pass, since it is missing one of the required parameters (count).此验证通过,如果我仅从架构中删除一个必需的部分,即使它不应该通过,我也不会收到错误消息,因为它缺少必需的参数之一(计数)。 When I put in different strings it aslo seems that none of the other "required" fields seem to be working even though they don't raise an error.当我输入不同的字符串时,似乎其他“必需”字段似乎都没有工作,即使它们没有引发错误。 What am I doing incorrectly here?我在这里做错了什么?

You have an extra properties keyword right below the "Apartments" properties declaration that shouldn't be there -- so everything below that is being parsed at the wrong level.您在“Apartments”属性声明的正下方有一个额外的properties关键字,该关键字不应该存在 - 所以下面的所有内容都在错误的级别解析。 I think you are intending that the properties "ap1", "ap2" and "ap3" should be at the same level in the data as "Apartments"?我认为您打算将属性“ap1”、“ap2”和“ap3”在数据中与“Apartments”处于同一级别?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM