You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用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,核心调整两点:

  1. 将type设置为["null", "array"],允许字段值为null或数组
  2. 使用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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:25:27