我正在使用Python/Flask编写一个程序,并使用一个名为flask-jsonschema-validator的包进行JSON验证。当我验证我的JSON时,我得到以下错误:
jsonschema.exceptions.SchemaError: [{'fieldname': {'type': 'string'}, 'type': {'type': 'string'}, 'description': {'type': 'string'}, 'default-value': {'type': 'string'}, 'validation': [{'type': 'string'}]}, {'fieldname': {'type': 'string'}, 'type': {'type': 'string'}, 'description': {'type': 'string'}, 'default-value': {'type': 'string'}, 'validation': [{'valid-values': {'type': 'string'}}, {'invalid-values': {'type': 'string'}}, {'isinteger': {'type': 'string'}}, {'nullable': {'type': 'string'}}]}] is not of type 'object', 'boolean'下面是我正在使用的JSON模式
{
"validate": {
"type": "object",
"properties": {
"_id": { "type": "string", "minLength": 2, "maxLength": 100 },
"name": { "type": "string", "minLength": 2, "maxLength": 100 },
"type": { "type": "string", "minLength": 2, "maxLength": 100 },
"subtype": { "type": "string", "minLength": 2, "maxLength": 100 },
"domain-data-version": {"type": "string"},
"description": { "type": "string", "minLength": 2, "maxLength": 100 },
"created" : {"type": "string"},
"owner-org": {"type": "string"},
"domain-data":[
{
"fieldname": {"type": "string"},
"type": {"type": "string"},
"description": {"type": "string"},
"default-value": {"type": "string"},
"validation": [{"type": "string"}]},
{"fieldname": {"type": "string"},
"type": {"type": "string"},
"description": {"type": "string"},
"default-value": {"type": "string"},
"validation": [ {"valid-values": {"type": "string"}},
{"invalid-values": {"type": "string"}},
{"isinteger": {"type": "string"}},
{"nullable": {"type": "string"}}]
}]
},
"required": []
}
}下面是我用来验证的JSON
{"name": "PHARMACY-CLAIM", "type": "Pharmacy", "subtype": "Prescription Filled", "domain-data-version": "1", "domain-data": [{"fieldname": "claim-id", "type": "string", "description": "The Insurance claim ID", "default-value": "null", "validation": [{"nullable": "false"}]}, {"fieldname": "member-gen-key", "type": "string", "description": "The unique insurance Member ID", "default-value": "null", "validation": [{"nullable": "false"}]}, {"fieldname": "ndc", "type": "string", "description": "The National Drug Code value for the medication prescription filled.", "default-value": "null", "validation": [{"nullable": "false"}]}]}当我删除模式的"domain-data“部分时,JSON将进行验证。我认为问题来自于domian-data是一个对象数组,但我不确定如何处理它。谢谢您的帮助。
发布于 2020-02-28 07:14:49
这里的第一个提示是,错误是一个SchemaError,这意味着您编写模式的方式存在问题,验证器甚至还没有花时间查看实例值。
第二个提示是错误消息列出了模式中"domain-data“属性的值,然后显示为... is not of type 'object', 'boolean'。虽然您指定了一个数组,但JSON Schema只允许模式作为"properties“关键字中的值-这需要一个对象(或布尔值)。
解决方案是修复模式中的"domain-data“属性,使其成为有效的子模式。
如果要让属性验证所有项都遵循相同架构的数组,请使用:
{ "type": "array", "items": {...} }如果您希望数组是一个元组,其中数组中的不同位置采用不同的模式,请使用:
{ "type": "array", "items": [ {first}, {second}, ... ] }如果这样做,请参考JSON Schema文档,了解"items“和"additionalItems”之间的区别。
发布于 2020-06-05 08:25:28
我和你有同样的问题。尽管这似乎是一个模式错误,但对我来说,这是JSON操作的一个问题。我使用的是字符串内容,而不是JSON对象。我已经构建了您的模式,并且它成功了。我将您的模式放在一个名为teste.json的文件中。
>>> from jsonschema import Draft7Validator
>>> import json
>>> Draft7Validator.check_schema(json.load(open('teste.json')))您可能没有将此内容作为JSON对象(json.load()).加载。仅供参考,Draft7Validator是一个用于验证JSON schema第7版的实现类。使用适合您需求的应用程序。
关于您的模式,我建议您遵循JSON schema文档。如果不使用definitions,我无法验证您的模式和实例。您的域数据似乎有两种验证方式。然后使用扩展将避免代码重复,如here所示。我重构了你的模式,它看起来像预期的那样工作。
{
"$schema": "http://json-schema.org/draft-06/schema#",
"$ref": "#/definitions/Validate",
"definitions": {
"Validate": {
"type": "object",
"additionalProperties": false,
"properties": {
"_id": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"name": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"type": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"subtype": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"domain-data-version": {
"type": "string"
},
"description": {
"type": "string",
"minLength": 2,
"maxLength": 100
},
"created": {
"type": "string"
},
"owner-org": {
"type": "string"
},
"domain-data": {
"type": "array",
"items": {
"$ref": "#/definitions/Domain-data"
}
}
}
},
"Domain-data": {
"type": "object",
"additionalProperties": false,
"properties": {
"fieldname": {
"type": "string"
},
"type": {
"type": "string"
},
"description": {
"type": "string"
},
"default-value": {
"type": "string"
},
"validation": {
"oneOf": [
{ "$ref": "#/definitions/DomainDataV1" },
{ "$ref": "#/definitions/DomainDataV2" }
]
}
}
},
"DomainDataV1": {
"type": "array",
"items": {
"type": "string"
}
},
"DomainDataV2": {
"type": "array",
"items": {
"type": "object",
"properties": {
"valid-values": {
"type": "string"
},
"invalid-values": {
"type": "string"
},
"isinteger": {
"type": "string"
},
"nullable": {
"type": "string"
}
}
}
}
}
}DomainDataV1和DomainDataV2是我认为的两种验证。当additionalProperties设置为false时,禁止任何其他属性。我希望它能有所帮助!;)
https://stackoverflow.com/questions/60441062
复制相似问题