我想使用 JavaScript 中的 Ajv 根据 JSON 模式验证 JSON。我收到错误:
抛出新的 it.MissingRefError(it.baseId, $schema, $message); ^ 错误:无法从 id requestGetGraphs 解析引用 #/definitions/requestGraph
删除对其他架构的引用时: {“$ref”:“#/definitions/requestGraph”} 错误消失。
JavaScript 代码:
ajv.addSchema(require('./json-schema/graph-response'), 'graph-response.json');
ajv.validate('requestGetGraphs', `{"type" : "requestGetGraphs", "space" : "config", "v" : 1.1, "id" : "dsaf" }`);
图表请求.json:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id" : "graph-requests.json",
"definitions": {
"requestGraph" : {
"$id" : "#/definitions/requestGraph",
"allOf" : [
{ "$ref" : "call.json/#/definitions/request" },
{
"properties": {
"space": {
"$id" : "#requestGraph/properties/space",
"type": "string",
"const": "config"
}
}
}
]
}
},
"requestGetGraphs" : {
"$id" : "requestGetGraphs",
"type" : "object",
"allOf" : [
{
"properties": {
"action": {
"$id" : "#requestGetGraphs/properties/action",
"type": "string",
"const": "requestGetGraphs"
}
}
},
{ "$ref" : "#/definitions/requestGraph" }
]
}
}
最佳答案
如果您来这里是为了解决与 @typescript-eslint
相关的构建错误 无法解析昨天突然出现的来自 id # 的引用 #/definitions/directiveConfigSchema
,只需将“@typescript-eslint/eslint-plugin”降级到版本“5.33.0”。如果您的 package.json 中没有它,只需添加它(版本中不带 ^ )。删除你的yarn.lock、package-lock.json和node_modules并重建。
更多信息请访问 https://github.com/typescript-eslint/typescript-eslint/issues/5525
关于javascript - 在同一文档中引用 $id 时,如何修复 "Can' t resolve reference"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55706424/