为什么 Ajv 在编译期间无法解析引用?
Why is Ajv unable to resolve reference during compile?
以下是我尝试编译并用于验证的 JSON 架构示例。为此,我使用 'ajv' npm module.
这是我 运行ning ...
的代码
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 #
还有其他人 运行 遇到过此类问题吗?知道我可能做错了什么吗?
您的引用不正确(虽然有效),应该是#/definitions/phone
或者,要使其正常工作,您可以在 phone 架构中添加 "id": "#definitions/phone"
,但更常见的是使用 "id": "#phone"
(并更新 $refs)。
以下是我尝试编译并用于验证的 JSON 架构示例。为此,我使用 'ajv' npm module.
这是我 运行ning ...
的代码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 #
还有其他人 运行 遇到过此类问题吗?知道我可能做错了什么吗?
您的引用不正确(虽然有效),应该是#/definitions/phone
或者,要使其正常工作,您可以在 phone 架构中添加 "id": "#definitions/phone"
,但更常见的是使用 "id": "#phone"
(并更新 $refs)。