【问题标题】:What does "$ref": "#" mean?"$ref": "#" 是什么意思?
【发布时间】:2020-09-25 01:50:53
【问题描述】:

以下是示例 JSON 架构代码: "$ref": "#" 指的是什么? 在这里吗?还是他们的意思是整个文件? 您可以在 https://www.jsonschemavalidator.net 上找到整个架构 选择 Schema Draft V7。

{
"$schema": "http://json-schema.org/draft-07/schema#",
 "definitions": {
    "schemaArray": {
        "type": "array",
        "minItems": 1,
        "items": { "$ref": "#" }
    },
    "nonNegativeInteger": {
        "type": "integer",
        "minimum": 0
    },
    "nonNegativeIntegerDefault0": {
        "allOf": [
            { "$ref": "#/definitions/nonNegativeInteger" },
            { "default": 0 }
        ]
    },....

【问题讨论】:

    标签: jsonschema ref


    【解决方案1】:

    我不是专家,但我有同样的问题,我想我找到了答案

    ... 每个 $ref 都被解析为一个完整的 URI。完成后,您的所有问题都会通过以下问题得到回答:如果我只是获取那个 URI,我最终会得到什么架构? $ref 在哪里,它是如何加载的,所有这些都是无关紧要的——它完全取决于解析的 URI。

    该库可能会采取一些捷径(例如缓存文档以便它们只获取一次,或者信任一个架构来“代表”另一个架构),但这些都是实现细节。

    # 并不特殊:$ref 的所有值都被解析为相对于当前文档的 URI(或“id”的最接近的值,如果有的话)。

    因此,如果您没有使用“id”,那么 # 将指向架构文档的根。如果您从 http://example.com/schema 获取您的架构,那么其中任何地方的 {"$ref": "#"} 都将解析为 http://example.com/schema#,即文档本身。

    链接:https://stackoverflow.com/a/17723151/989369

    所以基本上它指向主要方案,即

    {
    "$schema": "http://json-schema.org/draft-07/schema#",
     "definitions": {
        "schemaArray": {
            "type": "array",
            "minItems": 1,
            "items": { "$ref": "#" }
        }
    

    将转换为:

    {
    "$schema": "http://json-schema.org/draft-07/schema#",
     "definitions": {
        "schemaArray": {
            "type": "array",
            "minItems": 1,
            "items": {
              "$schema": "http://json-schema.org/draft-07/schema#",
              "definitions": {
              ...
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-12
      相关资源
      最近更新 更多