为什么Ajv在编译期间无法parsing引用?

以下是我试图编译和用于validation的JSON模式的示例。 为了完成这个,我使用'ajv'npm模块 。

这是我正在运行的代码…

var ajv = require('ajv')(); var contactSchema = { "$schema": "http://json-schema.org/draft-04/schema#", "title": "Contact", "type": "object", "additionalProperties": false, "properties": { "work": { "$ref": "#definitions/phone" }, "home": { "$ref": "#definitions/phone" }, }, "definitions": { "phone": { "type": "object", "required": ["number"], "properties": { "number": { "type": "string" }, "extension": { "type": "string" } } } } }; var validator = ajv.compile(contactSchema); 

当我运行这个代码,我得到以下例外..

 Error: can't resolve reference #definitions/phone from id # 

有没有人遇到这种问题? 任何想法我可能做错了什么?

你的参考是不正确的(虽然它是有效的),它应该是#/定义/电话

另外,为了使它工作,你可以在手机模式里添加"id": "#definitions/phone" ,但是使用"id": "#phone" "id": "#definitions/phone"更常见(也更新$ refs)。