我在一个使用Webpack的电子项目上工作。我使用json模式,配置如下:
module.exports = {
module: {
rules: [
{
test: /\.schema.json$/,
type: "asset/resource",
generator: {
filename: "schemas/[name].json",
},
}
]
}
}因此,如果我的源代码文件some-config.schema.json中有如下所示:
{
"$id": "/schemas/some-config",
"$ref": "/schemas/other-config"
}它以http://localhost:1212/dist/schemas/some-config.schema.json的形式发出。注意1212 --这是由配置构建的不同工具动态配置的端口号,因此在编写代码或模式时,我甚至不知道该路径。
但是,上面的$ref不起作用:
加载引用'file:///schemas/other-config':无法从‘/schemas/其他-config’加载模式‘:
失败时出现问题。
我希望它加载http://localhost:1212/dist/schemas/other-config.schema.json。我该如何设置这个引用?
发布于 2022-05-23 09:30:20
$ref的值是URI,而不是URL。这很重要,因为URI不一定是“网络可寻址”,也就是没有真正的网络地址。
$id的值仅为标识符。
当JSON解析器在$id中获得一个值(这是一个相对URI )时,如果它不能确定一个基本URI来解析为一个绝对URI,那么它应该是一个。所以,假装它是虚构的,或者使用https://example.com。
因此,当您使用$id值/schemas/other-config时,我们可以假装它是以https://example.com为前缀的,因此决定使用https://example.com/schemas/other-config。/schemas/some-config也是如此。
当JSON处理您提供的模式时,它应该首先使用URI解析过程,将引用与它的$id值索引匹配到模式。
基本上,正确设置模式文件可以定位在任何地方,只要它们具有正确的$id值,并且您已经将它们加载到正在使用的实现中。
https://stackoverflow.com/questions/72329253
复制相似问题