Python jsonschema库--架构无效?
我在Django应用程序中使用jsonschema库进行服务器端验证,并尝试使用提供的模式对JSON进行服务器端验证。但是,我得到一个错误,即该模式在任何给定模式下都无效 这是我的模式,它是类的模式属性的scores\u ap属性:Python jsonschema库--架构无效?,python,json,django,validation,jsonschema,Python,Json,Django,Validation,Jsonschema,我在Django应用程序中使用jsonschema库进行服务器端验证,并尝试使用提供的模式对JSON进行服务器端验证。但是,我得到一个错误,即该模式在任何给定模式下都无效 这是我的模式,它是类的模式属性的scores\u ap属性: class JSONListFieldSchemas: """ Schemas for all the JSON List Fields. Each key represents the field name. """ sch
class JSONListFieldSchemas:
"""
Schemas for all the JSON List Fields.
Each key represents the field name.
"""
schema = {
"scores_ap": {
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "AP Scores",
"type": "array",
"items": {
"type": "object",
"properties": {
"exam": {
"type": "string"
},
"score": {
"type": "integer",
"minimum": "1",
"maximum": "5",
"required": False
}
}
}
}
}
我得到这个错误:
{'type': 'object', 'properties': {'score': {'minimum': '1', 'type': 'integer', 'ma
ximum': '5', 'required': False}, 'exam': {'type': 'string'}}} is not valid under a
ny of the given schemas
Failed validating u'anyOf' in schema[u'properties'][u'items']:
{u'anyOf': [{u'$ref': u'#'}, {u'$ref': u'#/definitions/schemaArray'}],
u'default': {}}
On instance[u'items']:
{'properties': {'exam': {'type': 'string'},
'score': {'maximum': '5',
'minimum': '1',
'required': False,
'type': 'integer'}},
'type': 'object'}
我使用的模式如下:
from jsonschema import validate
from .schemas import JSONListFieldSchemas
raw_value = [{"score": 1, "exam": "a"}]
validate(raw_value, JSONListFieldSchemas.schema['scores_ap'])
从草稿4到草稿4,required应该是一个数组,而不是布尔值。 最大值和最小值也应该是整数,而不是字符串 试着这样做:
{
"$schema": "http://json-schema.org/draft-06/schema#",
"title": "AP Scores",
"type": "array",
"items": {
"type": "object",
"properties": {
"exam": {
"type": "string"
},
"score": {
"type": "integer",
"minimum": 1,
"maximum": 5
}
},
"required": [
"exam"
]
}
}
谢谢我的代码中的数据是一个数组,或者我声明它是错误的?是的,对不起,我的错误,没有仔细查看。我会更新我的答案。