You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSON Schema Draft 0.7是否支持嵌套定义(Definitions)?

关于JSON Schema Draft 07嵌套定义与$ref引用的解答

没问题,JSON Schema Draft 07完全支持嵌套定义,你想要的这种嵌套结构下的$ref引用是完全可行的,不过需要注意引用路径的写法细节~

核心要点:正确的JSON Pointer路径

在你的示例中,definitions里嵌套了link对象,其下又包含default和post两个子定义。要引用这些嵌套的子定义,你需要使用绝对JSON Pointer路径,格式为#/definitions/link/[子定义名称]——注意#后面必须紧跟/,然后按层级依次写出节点名称,这样才能准确定位到嵌套的子结构。

比如你想要的引用应该写成:

  • "$ref": "#/definitions/link/post"(引用link下的post定义)
  • "$ref": "#/definitions/link/default"(引用link下的default定义)

修正后的示例代码

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://api.thisisbeacon.com/schemas/",
  "definitions": {
    "link": {
      "default": {
        "$ref": "link/single.schema.json"
      },
      "post": {
        "$ref": "link/post/single.schema.json"
      }
    }
  },
  // 示例:在schema中使用这些嵌套定义
  "type": "object",
  "properties": {
    "submitLink": {
      "$ref": "#/definitions/link/post"
    },
    "regularLink": {
      "$ref": "#/definitions/link/default"
    }
  }
}

额外注意事项

  1. 字段命名:Draft 07中标准的标识字段是$id,而非你示例里的id(虽然部分工具可能兼容旧的id写法,但$id是官方推荐的标准用法)。
  2. 外部schema引用:你在嵌套定义里引用的外部schema(比如link/single.schema.json)是相对路径,它会基于当前schema的$id值来解析路径,这个逻辑是完全符合Draft 07规范的。

内容的提问来源于stack exchange,提问作者Barkermn01

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:23:48