【发布时间】: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