是否存在可验证JSON数组Schema定义的公开JSON Schema?
验证数组类型JSON Schema的方案
当然可以实现这个需求——你可以基于JSON Schema的元规范,定制一个专门用来验证「数组类型Schema」的Schema。核心思路是先确保目标Schema符合JSON Schema的基本格式,再强制它满足数组类型的特定要求(比如指定type为array、包含name和items字段)。
下面是一个直接可用的Schema示例,针对JSON Schema Draft 07版本:
{ "type": "object", "required": ["type", "name", "items"], "properties": { "type": { "type": "string", "enum": ["array"] }, "name": { "type": "string" }, "items": { "oneOf": [ { "$ref": "#" }, { "type": "array", "items": { "$ref": "#" } } ] } }, "additionalProperties": true }
关键规则说明:
required数组强制要求目标Schema必须包含type、name、items三个字段,满足你说的「超集」基础要求type字段被限定为字符串"array",确保这是一个数组类型的Schema定义items字段支持两种合法格式:单个JSON Schema对象,或者多个Schema对象组成的数组(符合JSON Schema原生对items的定义)additionalProperties: true允许目标Schema包含其他额外字段(比如description、minItems等),保证它是数组Schema的超集
示例验证:
- 符合要求的Schema:
{ "name": "tags", "type": "array", "items": { "type": "string" }, "minItems": 1 }
- 不符合要求的情况:
- 缺少
items字段 → 验证失败 type设为"string"→ 验证失败name是数字而非字符串 → 验证失败
- 缺少
这个方案完全基于JSON Schema原生规范,不需要依赖外部工具或第三方Schema,直接就能用来验证用户提交的数组类型Schema定义。
内容的提问来源于stack exchange,提问作者John Teague
相关产品推荐
相关产品推荐

