我正在使用 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"}]}]}
当我删除架构的“域数据”部分时,JSON 将验证。我认为问题来自于 domian-data 是一个对象数组,但我不知道该怎么办。谢谢寻求帮助。
最佳答案
对于阅读本文的其他人,并且您正在使用数组,请尝试“prefixItems”而不是“items”。查看此链接以获得更多感受。 https://json-schema.org/understanding-json-schema/reference/array.html
关于python - 如何格式化 JSON 模式以进行验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60441062/