【问题标题】:JSON schema failed to resolve $ref, Expected StartObject Bolean, got StringJSON 模式无法解析 $ref,需要 StartObject 布尔值,得到字符串
【发布时间】:2020-06-28 04:24:02
【问题描述】:

$ref 无法解析另一个文件中的子模式

错误信息: Unexpected token encountered when reading value for '$ref'. Expected StartObject, Boolean, got String. Path 'properties.organization.items.properties.$ref'

根架构

{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "$id": "https://example.com/schema_root.json",
    "type": "object",
    "properties": {
        "organization": {
            "description": "Organization information associated with the sample",
            "type": "array",
            "items": {
                    "$ref": "organisation.json#"
            }
        }
    }
}

子模式在文件organisation.json的同一目录中

{
    "$id": "organisation",
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": "Organisation",
    "additionalProperties": false,
    "type": "object",
    "properties": {
        "Name": {
            "type": "string"
        },
        "Role": {
            "type": "string"
        }
    }
}

在我看来,根模式可以找到子模式。但是,由于某些数据类型的差异,它无法加载子模式。但我不确定差异是什么?

在错误消息Expected StartObject.got String 我不确定StartObject 是什么以及String 指的是什么。

【问题讨论】:

  • 这可能是特定验证器的问题。您使用的是哪个库?
  • 我尝试了几个验证器,包括 Python jsonschema、AJV。错误消息略有不同,但都指向“字符串”问题。我想知道“$ref”中是否有一些数据类型限制
  • 删除$ref 值末尾的哈希会发生什么?此外,文件夹结构中的文件中的模式的位置通常是未知的或不被实施考虑考虑。在运行验证之前,您必须添加或加载您希望引用的其他模式。引用解析仅适用于 URI。您的其他架构的 $id 需要是完整的 URI。在这种情况下,它需要匹配引用模式基 URI 的域部分。
  • 删除哈希没有改变。我尝试将 subschema 的 $id 更改为 URL “www.example.com/subschema.json” 并引用根架构中的“$id”值“$ref”:“example.com/subschema.json(#)” 错误消息是cant resolve the reference 'https://...subschema.json' from id 'https://...subschema.json#' 会不会和最后一个hash有关?

标签: json validation schema jsonschema python-jsonschema


【解决方案1】:

有两个问题。

  1. 引用路径上有扩展名 (.json),但标识符路径上没有。
  2. 根据 $id 解析引用,更改您识别第二个架构的方式。

希望这不是太简洁以至于无法理解,但这就是发生的事情。

// From Root Schema
"$id":  "http://example.com/schema_root.json"
"$ref": "organization.json#"

// $ref resolves to ...
"$ref": "http://example.com/organization.json#"

// Expected $id
"$id": "http://example.com/organization.json"

// Actual $id
"$id":                    "organization"

引用中的标识符与第二个架构中的 $id 不匹配,因此验证器找不到它需要的架构。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多