jsonschema 库——架构无效?
jsonschema library -- schema is not valid?
我在我的 Django 应用程序中使用 jsonschema 库 (http://python-jsonschema.readthedocs.io/en/latest/validate/) 进行服务器端验证,并尝试使用提供的模式对 JSON 进行服务器端验证。但是,我收到架构 "is not valid under any of the given schemas."
的错误
这是我的架构(它是 class 的 "schema" 属性 的 "scores_ap" 属性:
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 稿开始,"required" 应该是数组而不是布尔值。
另外 "maximum" 和 "minimum" 应该是整数,而不是字符串。
这样试试:
{
"$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"
]
}
}
我在我的 Django 应用程序中使用 jsonschema 库 (http://python-jsonschema.readthedocs.io/en/latest/validate/) 进行服务器端验证,并尝试使用提供的模式对 JSON 进行服务器端验证。但是,我收到架构 "is not valid under any of the given schemas."
的错误这是我的架构(它是 class 的 "schema" 属性 的 "scores_ap" 属性:
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 稿开始,"required" 应该是数组而不是布尔值。 另外 "maximum" 和 "minimum" 应该是整数,而不是字符串。
这样试试:
{
"$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"
]
}
}