JSONSchema -必需的属性不工作



我是JSON的新手,如果我错过了一些非常简单的东西,我不会感到惊讶,但是我尝试并未能找到我在模式中究竟做错了什么,以及为什么它验证一些事情不正确。这是我的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"]
}

我试图通过使用json验证字符串。加载,然后验证函数对这个模式,但当我尝试这个,我得到这个消息:

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

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

在"Apartments"下面有一个额外的properties关键字属性声明不应该在那里——所以下面的所有内容都在错误的级别上被解析。我想你的意思是属性";ap1;;ap2;;"one_answers";ap3"在数据中应该与"公寓"处于同一水平?

最新更新