如何使用jsonschema验证含None值的JSON数组数据
JSON Schema验证:允许数组字段为None并在非空时验证结构
需求与问题
需要验证JSON中的数组字段,规则如下:
- 字段值为
None(对应JSON中的null)时可通过验证 - 字段不为
None时,必须是数组,且数组内每个对象都包含指定必填字段
现有Schema验证含None的data2时抛出错误:ValidationError: "None is not of type 'array'",需调整Schema实现需求。
原有代码
from jsonschema import validate data = { "coffee": { "region": [{"id": 1,"name": "John Doe"},{"id": 2,"name": "Don Joeh"}], "country": {"id": 2,"company": "ACME"} }, "brewing": { "region": [{"id": 1,"name": "John Doe"},{"id": 2,"name": "Don Joeh"}], "country": {"id": 2,"company": "ACME"} } } schema = { "type": "object", "required": ["coffee", "brewing"], "additionalProperties": False, "properties": { "coffee": { "type": "object", "required": ["region", "country"], "properties": { "region": { "type": "array", "required":False, "items": { "type": "object", "required": ['id', 'name'], "properties": { "id": {"type": "number"}, "name": {"type": "string"} } } }, "country": { "type": "object", "required": ["id", "company"], "properties": { "id": {"type": "number"}, "company": {"type": "string"} } } } }, "brewing": { "type": "object", "required": ['region', 'country'], "properties": { "region": { "type": "array", "items": { "type": "object", "required": ["id", "name"], "properties": { "id": {"type": "number"}, "name": {"type": "string"} } } }, "country": { "type": "object", "required": ["id", "company"], "properties": { "id": {"type": "number"}, "company": {"type": "string"} } } } } } } data2 = { "coffee": { "region": None, "country": {"id": 2,"company": "ACME"} }, "brewing": { "region": [{"id": 1,"name": "John Doe"}, {"id": 2,"name": "Don Joeh"}], "country": {"id": 2,"company": "ACME"} } }
解决方案
修改region字段的Schema,核心调整两点:
- 将
type设置为["null", "array"],允许字段值为null或数组 - 使用
if/then条件规则:仅当字段类型是数组时,验证数组项的结构
修改后的完整代码:
from jsonschema import validate # 更新后的Schema updated_schema = { "type": "object", "required": ["coffee", "brewing"], "additionalProperties": False, "properties": { "coffee": { "type": "object", "required": ["region", "country"], "properties": { "region": { "type": ["null", "array"], # 允许null或array类型 "if": {"type": "array"}, "then": { # 当为数组时,强制验证项结构 "items": { "type": "object", "required": ['id', 'name'], "properties": { "id": {"type": "number"}, "name": {"type": "string"} } } } }, "country": { "type": "object", "required": ["id", "company"], "properties": { "id": {"type": "number"}, "company": {"type": "string"} } } } }, "brewing": { "type": "object", "required": ['region', 'country'], "properties": { "region": { "type": ["null", "array"], # 同样允许null或array "if": {"type": "array"}, "then": { "items": { "type": "object", "required": ["id", "name"], "properties": { "id": {"type": "number"}, "name": {"type": "string"} } } } }, "country": { "type": "object", "required": ["id", "company"], "properties": { "id": {"type": "number"}, "company": {"type": "string"} } } } } } } # 验证两个数据实例 validate(instance=data, schema=updated_schema) # 无报错 validate(instance=data2, schema=updated_schema) # 无报错
说明
type: ["null", "array"]:明确允许字段值为null(对应Python中的None)或数组类型if/then规则:只有当字段是数组时,才会触发数组项的结构验证,确保非空数组的每个对象都包含id和name必填字段
内容的提问来源于stack exchange,提问作者soumya ranjan
相关产品推荐
相关产品推荐

