required 和 additionalProperties 不适用于 jsonschema 模块 Python
required and additionalProperties is not working for jsonschema module Python
我有以下代码:
from jsonschema import validate
schema_data = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
"additional" : {"type" : "number"},
},
"required": ["price", "name", "additional"],
"additionalProperties": False
}
json_data = {"name" : "Eggs", "price" : 34.99, "new": 90}
对于 required
和 additionalProperties
这应该给我一个错误,因为 additional
不存在于 json_data
中并且 new
不存在于 schema_data
。 但是脚本没有给出任何错误。
我需要额外安装吗?我有以下配置:
Python 2.7.12,
jsonschema==3.0.1
attrs==19.1.0
six==1.12.0
pyrsistent==0.14.11
您需要运行下面的命令才能得到错误
validate(json_data, schema_data)
所以首先你会得到错误
'additional' is a required property
修复后你会得到
的错误
Additional properties are not allowed ('new' was unexpected)
我有以下代码:
from jsonschema import validate
schema_data = {
"type" : "object",
"properties" : {
"price" : {"type" : "number"},
"name" : {"type" : "string"},
"additional" : {"type" : "number"},
},
"required": ["price", "name", "additional"],
"additionalProperties": False
}
json_data = {"name" : "Eggs", "price" : 34.99, "new": 90}
对于 required
和 additionalProperties
这应该给我一个错误,因为 additional
不存在于 json_data
中并且 new
不存在于 schema_data
。 但是脚本没有给出任何错误。
我需要额外安装吗?我有以下配置:
Python 2.7.12,
jsonschema==3.0.1
attrs==19.1.0
six==1.12.0
pyrsistent==0.14.11
您需要运行下面的命令才能得到错误
validate(json_data, schema_data)
所以首先你会得到错误
'additional' is a required property
修复后你会得到
的错误Additional properties are not allowed ('new' was unexpected)