4

以下是我尝试编译并用于验证的 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 #

有没有其他人遇到过这种问题?知道我可能做错了什么吗?

4

1 回答 1

3

您的参考不正确(虽然它是有效的),它应该是 #/definitions/phone

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

于 2016-10-28T15:41:02.293 回答