0

无法解析另一个文件中的子模式$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指的是什么。

4

1 回答 1

0

有两个问题。

  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,因此验证器找不到它需要的模式。

于 2020-03-17T14:55:12.780 回答