英文:
Why is it valid jsonschema for $id to be a path (i.e. end with a /)
问题
以下是您请求的翻译内容:
jsonschema测试套件包含许多测试用例。
这个测试用例演示了通过"$id"字段进行基本URI更改,将子模式中的基本URI从#/items
更改为http://localhost:1234/draft2020-12/baseUriChange/
。
显然,这应该测试下面的引用是否解析为http://localhost:1234/draft2020-12/baseUriChange/folderInteger.json
。
令人困惑的部分是#/items
下的模式现在是通过路径引用的(请注意末尾的斜杠),而不是文件。因此,它的规范URI http://localhost:1234/draft2020-12/baseUriChange/
从语义上来说是一个路径,但它引用了具有内容的实际对象(模式)。
我觉得这对我来说没有意义,我觉得很令人困惑。
JSON模式实际上允许这样做吗(我猜是吗)?这在使用URI的地方是否常见,或者是否有任何这种决定的理由?
编辑:原则上,可以通过提供文件名来实现相同的基本URI更改功能。
然后,"$ref"仍然会解析为http://localhost:1234/draft2020-12/baseUriChange/folderInteger.json
。那么为什么可以省略文件名呢?
请参阅相对URI示例。
英文:
The jsonschema test suite contains many test cases.
This test case
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "http://localhost:1234/draft2020-12/",
"items": {
"$id": "baseUriChange/",
"items": {"$ref": "folderInteger.json"}
}
demonstrates a base-uri change with the "$id" field, which changes the base uri of the sub-schema
in #/items
to http://localhost:1234/draft2020-12/baseUriChange/
.
Clearly this should test if the below reference resolves to http://localhost:1234/draft2020-12/baseUriChange/folderInteger.json
.
The confusing part is however that the schema under #/items
is now referred to by a path (note the trailing slash) and not a file. So its canonical uri http://localhost:1234/draft2020-12/baseUriChange/
is a semantically a path, but refers to an actual object with content (the schema).
This makes no sense to me and I find it confusing.
Is this actually allowed in jsonschema (I guess so)? And it is common in places where uris are used or is there any rationale for this descision?
Edit: In principle the same base uri change functionality can be achieved by giving file names
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "http://localhost:1234/draft2020-12/abc",
"items": {
"$id": "baseUriChange/xyz",
"items": {"$ref": "folderInteger.json"}
}
Then the "$ref" will still resolve to http://localhost:1234/draft2020-12/baseUriChange/folderInteger.json
. So why is it ok to omit file names?
答案1
得分: 1
测试中包含文件夹路径存在是因为这是可以(而且可能已经)完成的操作。
实际上有很多这些边缘案例测试与最佳实践不一致。测试套件的目的不是要说明如何编写模式,而是要确保在奇怪的情况下也能保持实施行为。
显然不建议这样做,但测试确保实现在发生这种情况时能够正确处理这些事情。
英文:
The test with a folder path exists because it's something that can be (and probably has been) done.
There are a lot of these edge case tests that don't align with best practice, actually. The test suite isn't supposed to illustrate how to write schemas. It's intended to ensure implementation behavior, even in weird scenarios.
It's not recommended to do these things, obviously, but the tests ensure implementations handle such things properly if they do happen.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论