如何使 json $ref本地文件?

我正在我的节点.js项目中使用 AJV 包。


我正在尝试根据几个架构文件验证一些数据。这两个架构文件位于同一目录中:


/dir

    |

    parent_schema.json

    |

    sub_schema.json

/data

    |

    data.json


我试图得到一个超级简单的例子,但我遇到了麻烦。 看来:$refparent_schema.json


{

  "properties": {

    "foo": { "type": "string" },

    "bar": { "$ref": "sub_schema.json" }

  }

}

看起来像这样:sub_schema.json


{

  "properties": {

    "sub1": { "type": "string" },

  }

}

为了完整起见,我试图验证我的:如下所示:data.json


{

  "foo": "whatever",

  "bar": {

    "sub1": "sometext"

  }

}

我遇到的问题是我的道路。我从 AJV 收到此错误:$ref


MissingRefError {

    message: "can't resolve reference subschema1.json from id #"

    missingRef: "subschema1.json"

    missingSchema: "subschema1.json"

}

有人看到我的道路出了什么问题吗?我知道您还应该使用 来选择要匹配的特定属性,但我希望使用整个架构。#


手掌心
浏览 104回答 1
1回答

缥缈止盈

这是一个常见的误解,以某种方式“加载”文件。$ref看看&nbsp;ajv.js.org&nbsp;是怎么说的:$ref使用架构$id作为基本 URI 作为 URI 引用进行解析(请参阅示例)。和:您不必在用作架构$id的 URI 上托管架构文件。这些 URI 仅用于标识架构,根据 JSON 架构规范,验证程序不应期望能够从这些 URI 下载架构。Ajv 不会尝试从以下位置加载此架构:例如:stack://over.flow/string{&nbsp; "$id": "stack://over.flow/string",&nbsp; "type": "string"}如果要在另一个架构中引用该架构,它们都需要具有相同的基本URI,例如,stack://over.flow/{&nbsp; "$id":&nbsp; "stack://over.flow/object",&nbsp; "type": "object",&nbsp; "properties": {&nbsp; &nbsp; "a": { "$ref": "string#" }&nbsp; }}这里说“在 stack://over.flow/string 导入架构”,所以你最终得到:{ "$ref": "string#" }{&nbsp; "$id":&nbsp; "stack://over.flow/object",&nbsp; "type": "object",&nbsp; "properties": {&nbsp; &nbsp; "a": {&nbsp; &nbsp; &nbsp; "$id": "stack://over.flow/string",&nbsp; &nbsp; &nbsp; "type": "string"&nbsp; &nbsp; }&nbsp; }}这允许您组合小架构:const ajv = new Ajv;ajv.addSchema({&nbsp; "$id": "stack://over.flow/string",&nbsp; "type": "string"});ajv.addSchema({&nbsp; "$id": "stack://over.flow/number",&nbsp; "type": "number"});const is_string = ajv.getSchema("stack://over.flow/string");const is_number = ajv.getSchema("stack://over.flow/number");console.log(is_string('aaa'), is_string(42));console.log(is_number('aaa'), is_number(42));const is_ab = ajv.compile({&nbsp; "$id":&nbsp; "stack://over.flow/object",&nbsp; "type": "object",&nbsp; "properties": {&nbsp; &nbsp; "a": { "$ref": "string#" },&nbsp; &nbsp; "b": { "$ref": "number#" }&nbsp; }});console.log(is_ab({a: "aaa", b: 42}));console.log(is_ab({a: 42, b: "aaa"}));<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.2/ajv.min.js"></script>(请注意,在您的示例中,两个架构都不正确。两者都缺少 {“type”: “对象”}。要回答您的问题:const ajv = new Ajv;ajv.addSchema({&nbsp; "$id": "stack://over.flow/parent.schema",&nbsp; "type": "object",&nbsp; "properties": {&nbsp; &nbsp; "foo": { "type": "string" },&nbsp; &nbsp; "bar": { "$ref": "child.schema#" }&nbsp; }});ajv.addSchema({&nbsp; "$id": "stack://over.flow/child.schema",&nbsp; "type": "object",&nbsp; "properties": {&nbsp; &nbsp; "sub1": { "type": "string" },&nbsp; }});const is_parent = ajv.getSchema("stack://over.flow/parent.schema");const is_child = ajv.getSchema("stack://over.flow/child.schema");console.log(is_parent({&nbsp; "foo": "whatever",&nbsp; "bar": {&nbsp; &nbsp; "sub1": "sometext"&nbsp; }}));<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.2/ajv.min.js"></script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript