node.js - 为什么 Ajv 在编译期间无法解析引用?

标签 node.js jsonschema ajv

以下是我尝试编译并用于验证的 JSON 架构示例。为此,我使用了 'ajv' npm module .

这是我正在运行的代码......

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

或者,要使其正常工作,您可以在电话模式中添加 "id": "#definitions/phone",但使用 "id": "#phone 更常见"(也更新 $refs)。

关于node.js - 为什么 Ajv 在编译期间无法解析引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40306394/

相关文章:

node.js - Google App Engine 上的私有(private) Node 模块

javascript - 使用 grunt.js 生成动态文件名

javascript - 在 sequelize 上, "include"的 "findOne"不工作

jsonschema - 如何将 JSON Schema $ref 解析为父目录中的文件

javascript - Selenium 通过 Node.js 和 webdriverIO : timeout without effect

python - jsonschema 通过具有不同名称或类型的键进行验证

jsonschema - 是 XML 架构 xs :alternative equivalent available in JSON Schema?

json - 使用对象属性键作为 JSON 模式中的枚举

arrays - 如何使用 ajv 验证空字符串数组?