javascript - 在同一文档中引用 $id 时,如何修复 "Can' t resolve reference"错误?

标签 javascript jsonschema ajv

我想使用 JavaScript 中的 Ajv 根据 JSON 模式验证 JSON。我收到错误:

抛出新的 it.MissingRefError(it.baseId, $schema, $message); ^ 错误:无法从 id requestGetGraphs 解析引用 #/definitions/requestGraph

删除对其他架构的引用时: {“$ref”:“#/definitions/requestGraph”} 错误消失。

JavaScript 代码:

ajv.addSchema(require('./json-schema/graph-response'), 'graph-response.json');
ajv.validate('requestGetGraphs', `{"type" : "requestGetGraphs", "space" : "config", "v" : 1.1, "id" : "dsaf" }`);

图表请求.json:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id" : "graph-requests.json",
  "definitions": {
    "requestGraph" : {
      "$id" : "#/definitions/requestGraph",
      "allOf" : [
        { "$ref" : "call.json/#/definitions/request" },
        {
          "properties": {
            "space": {
              "$id" : "#requestGraph/properties/space",
              "type": "string",
              "const": "config"
            }
          }
        }
      ]
    }
  },
  "requestGetGraphs" : {
      "$id" : "requestGetGraphs",
      "type" : "object",
      "allOf" : [
        {
          "properties": {
            "action": {
              "$id" : "#requestGetGraphs/properties/action",
              "type": "string",
              "const": "requestGetGraphs"
            }
          }
        },
        { "$ref" : "#/definitions/requestGraph" }
      ]

    }

}

最佳答案

如果您来这里是为了解决与 @typescript-eslint 相关的构建错误 无法解析昨天突然出现的来自 id # 的引用 #/definitions/directiveConfigSchema,只需将“@typescript-eslint/eslint-plugin”降级到版本“5.33.0”。如果您的 package.json 中没有它,只需添加它(版本中不带 ^ )。删除你的yarn.lock、package-lock.json和node_modules并重建。

更多信息请访问 https://github.com/typescript-eslint/typescript-eslint/issues/5525

关于javascript - 在同一文档中引用 $id 时,如何修复 "Can' t resolve reference"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55706424/

相关文章:

javascript - 计算多个输入数字的总和 JQuery

c# - 从大型 OpenAPI 文档或使用 NewtonSoft 获取 JSON 模式并解析 refs

java - Jackson JSON 模式模块不支持 JSON 模式草案 v4 吗?

javascript - 使用 AJV 解析无效对象的自定义架构

c# - jQuery REST POST JSON 列表<对象>

javascript - 如何在 Google Chart Tools 中连接两点 - 散点图

javascript - AJV - 引用外部模式而不先添加它

javascript - Ajv:使用动态键验证 json

javascript - 如何更改警告框的样式?

.net - 如何验证 JsonSchema 类型属性中的多个 $ref