JSONSchema Draft7数组子定义验证失败问题求助
问题诊断与修复
我一眼就发现了问题的核心:你的JSON Schema(Draft 7)中,嵌套层级的引用关键字使用错误,导致验证器无法识别第二层及以下的结构定义。
具体错误点
JSON Schema中引用定义必须使用标准关键字 $ref,但你的Schema里有两处误写为小写的ref(缺少美元符号$):
- 在
Organisation定义的users字段里:
应该改为:"users": { "type": "array", "items": { "ref": "#/definitions/User" } }"users": { "type": "array", "items": { "$ref": "#/definitions/User" } } - 在
User定义的reports字段里:
应该改为:"reports": { "type": "array", "items": { "ref": "#/definitions/Report" } }"reports": { "type": "array", "items": { "$ref": "#/definitions/Report" } }
修正后的完整Schema
{ "$schema": "http://json-schema.org/draft-07/schema#", "$id": "http://myorg/json/schemas/report-schedule.json", "type": "array", "items": { "$ref": "#/definitions/Organisation" }, "definitions": { "Organisation": { "type": "object", "required": [ "organisationId", "organisationName", "users" ], "properties": { "organisationId": { "type": "string", "minLength": 1 }, "organisationName": { "type": "string", "minLength": 1 }, "users": { "type": "array", "items": { "$ref": "#/definitions/User" } } } }, "User": { "type": "object", "required": [ "name", "email", "reports" ], "properties": { "name": { "type": "string", "minLength": 1 }, "email": { "type": "string", "format": "email" }, "reports": { "type": "array", "items": { "$ref": "#/definitions/Report" } } } }, "Report": { "type": "object", "required": [ "reportType", "reportWeekEndDay", "sendDay", "sendHour", "locations", "widgets" ], "properties": { "reportType": { "type": "string", "enum": [ "org-weekly" ] }, "reportWeekEndDay": { "type": "integer", "maximum": 6, "minimum": 0 }, "sendDay": { "type": "integer", "maximum": 6, "minimum": 0 }, "sendHour": { "type": "integer", "maximum": 23, "minimum": 0 }, "locations": { "type": "array", "items": { "type": "string", "minLength": 1 } }, "widgets": { "$ref": "#/definitions/Widgets" } } }, "Widgets": { "type": "array", "title": "Widgets within a report", "items": { "$ref": "#/definitions/Widget" } }, "Widget": { "type": "object", "required": [ "widgetType", "metrics", "comparisonType" ], "properties": { "widgetType": { "type": "string", "enum": [ "table-individual-locations", "table-all-locations" ] }, "comparisonType": { "type": "string", "enum": [ "week-on-week", "3-and-1-month-weekly-avg" ] }, "metrics": { "$ref": "#/definitions/Metrics" } } }, "Metrics": { "type": "array", "title": "The Available Metrics", "items": { "oneOf": [ { "$ref": "#/definitions/FootFallAndMovement" }, { "$ref": "#/definitions/Engagement" }, { "$ref": "#/definitions/Occupancy" }, { "$ref": "#/definitions/SalesDataTransactions" }, { "$ref": "#/definitions/SalesDataConversion" } ] } }, "FootFallAndMovement": { "type": "object", "required": [ "title", "type", "endpoint", "queryParams" ], "properties": { "title": { "type": "string", "minLength": 1 }, "type": { "type": "string", "const": "FootFallAndMovement" }, "endpoint": { "type": "string", "minLength": 1 }, "queryParams": { "type": "object", "required": [ "excludeStaff" ], "properties": { "excludeStaff": { "type": "boolean" } } } } }, "Engagement": { "type": "object", "required": [ "title", "type", "endpoint", "queryParams" ], "properties": { "title": { "type": "string", "minLength": 1 }, "type": { "type": "string", "const": "Engagement" }, "endpoint": { "type": "string", "minLength": 1 }, "queryParams": { "type": "object", "required": [ "excludeStaff", "contexts" ], "properties": { "additionalFilters": { "type": "object", "required": [ "ignoreLingerUnder" ], "properties": { "ignoreLingerUnder": { "type": "integer", "minimum": 1 } } }, "excludeStaff": { "type": "boolean" }, "contexts": { "type": "object", "required": [ "type", "value" ], "properties": { "type": { "type": "string", "const": "taxonomy" }, "value": { "type": "string", "minLength": 1 } } } } } } }, "Occupancy": { "type": "object", "required": [ "title", "type", "endpoint", "queryParams" ], "properties": { "title": { "type": "string", "minLength": 1 }, "type": { "type": "string", "const": "Occupancy" }, "endpoint": { "type": "string", "minLength": 1 }, "queryParams": { "type": "object", "required": [ "excludeStaff", "contexts" ], "properties": { "excludeStaff": { "type": "boolean" }, "contexts": { "type": "object", "required": [ "type", "value" ], "properties": { "type": { "type": "string", "const": "taxonomy" }, "value": { "type": "string", "minLength": 1 } } } } } } }, "SalesDataTransactions": { "type": "object", "required": [ "title", "type", "endpoint" ], "properties": { "title": { "type": "string", "minLength": 1 }, "type": { "type": "string", "const": "SalesDataTransactions" }, "endpoint": { "type": "string", "minLength": 1 } } }, "SalesDataConversion": { "type": "object", "required": [ "title", "type", "endpoint", "queryParams" ], "properties": { "title": { "type": "string", "minLength": 1 }, "type": { "type": "string", "const": "SalesDataConversion" }, "endpoint": { "type": "string", "minLength": 1 }, "queryParams": { "type": "object", "required": [ "excludeStaff" ], "properties": { "excludeStaff": { "type": "boolean" } } } } } } }
验证效果
修正后,你的示例数据里的所有错误都会被正确捕获:
- 用户对象里的
nameWITHERROR字段缺失了必填的name字段 reportType的值org-weekly1不在枚举列表中- 第一个metric的
type值FootFallAndMovement1不符合const约束
内容的提问来源于stack exchange,提问作者InTooDeep
相关产品推荐
相关产品推荐

