【问题标题】:MissingRefError - json schema remote REST Webservice enum using $ref doesnt resolve reference to the remote jsonMissingRefError - 使用 $ref 的 json 架构远程 REST Web 服务枚举无法解析对远程 json 的引用
【发布时间】:2019-07-28 10:31:56
【问题描述】:

我正在使用 Angular6-json-schema-form 从 JSON 模式生成 Angular 表单。

我正在尝试使用引用“$ref”从远程 REST Web 服务填充架构枚举,但我收到 MissingRefError。

这是我的架构:

{
"$id": "http://www.mocky.io/v2/5c7ff2e833000000338484c2.json#",
"title": "A rather large form",
"type": "object",
"properties": {
  "noenum": { "$ref": "#/definitions/largeEnum" }
}
}

链接内的 JSON 有以下内容:

{ 
"definitions": {
"largeEnum": {
  "type": "string",
  "enum": [
    "option #0",
    "option #1",
    "option #2",
    "option #3",
    "option #4"
  ]
}
}
}

如果我在本地这样做,它会起作用:

{
"definitions": {
  "largeEnum": {
    "type": "string",
    "enum": [
        "option #0",
        "option #1",
        "option #2",
        "option #3",
        "option #4"
    ]
  },
  "title": "A rather large form",
  "type": "object",
  "properties": {
    "noenum": {"$ref": "#/definitions/largeEnum" }
   }
}

请看错误截图

我需要让它从托管的 json 文件或 REST 端点远程工作。

【问题讨论】:

  • "JSON 里面的链接有以下内容:" - 什么链接?你能解释一下你的意思吗?
  • “如果我在本地这样做,它会起作用:” - 如果你在本地做什么?你能提供一些代码来具体演示你在本地做什么吗?
  • @Relequestual 我的意思是在本地对 /definitions/largeEnum 的引用 '$ref'(定义在同一架构上提供。)现在外部链接中的 json (mocky.io) 在 '$ id' 属性。你怎么没看到我刚刚提供的代码(json schemas和错误截图)
  • 我可以很好地看到错误。我想看看你编写和运行的代码,因为这会告诉我你实际做了什么,而不是我认为你可能做了什么。
  • 看起来 Erwin 可能是正确的。你认为$id 是做什么的?根据 Erwin 的回答,$id 用于标识当前模式。

标签: json forms rest jsonschema ref


【解决方案1】:

从您的评论来看,您似乎认为 $id 解析了远程引用。但这不正确; $id 定义您可以引用当前模式的方式。

您需要使用 "$ref" 关键字来引用远程架构。

(您的验证器是主动检索远程 URL,还是要求您自己包含该架构的内容,取决于验证器的实现。)

你可以这样做:

{
    "title": "A rather large form",
    "type": "object",
    "properties": {
        "noenum": {
            "$ref": "http://www.mocky.io/v2/5c7ff2e833000000338484c2.json#/definitions/largeEnum"
        }
    }
}

【讨论】:

  • 感谢您的回答。我正在使用的库不支持 $ref 到外部链接“(您的验证器是否主动检索远程 URL,或者要求您自己包含该架构的内容,取决于验证器的实现。)”
  • 您能否建议替代库或实现外部架构功能的 $ref 的方法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2013-10-24
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 2018-05-16
  • 1970-01-01
相关资源
最近更新 更多