如何正确引用外部JSON Schema?DNS工具Schema编译报错求助
问题解决方法
你的JSON Schema存在两处问题导致AJV编译报错,以下是修正方案:
1. 修复JSON语法错误
你在items字段中多嵌套了一层不必要的大括号,正确写法应直接将$ref放在items的对象内,无需额外包裹:
修正后的dns-tool.schema.json代码:
{ "$schema": "http://json-schema.org/draft-07/schema#", "$id": "dns-tool.schema.json", "title": "Zone Config Schema", "type": "object", "properties": { "records": { "type": "array", "items": { "$ref": "dns-record.schema.json" } } } }
2. 解决AJV的引用解析问题
即使修复语法,AJV仍可能无法自动识别同目录下的依赖Schema,你需要在编译命令中通过-r参数显式指定要加载的引用文件:
ajv compile --verbose true -s dns-tool.schema.json -r dns-record.schema.json
如果不想每次编译都手动指定依赖,也可以给dns-record.schema.json设置一个内部片段ID(比如"$id": "#dns-record"),然后在主Schema中用"$ref": "#dns-record"引用,但显式通过-r加载依赖是更直接的方案。
内容的提问来源于stack exchange,提问作者Tom
相关产品推荐
相关产品推荐

