我正在尝试使用 $ref
从另一个 json 模式导入定义。但出现以下错误:can't resolve reference ../base/definitions.schema.json#/definitions/datetime from id #
{
"$schema": "http://json-schema.org/draft-06/schema#",
“定义”:{
“约会时间”: {
“类型”:“字符串”
},
“姓名”: {
“类型”:“字符串”
},
}
}
{
"$schema": "http://json-schema.org/draft-06/schema#",
“特性”: {
“事件”:{“类型”:“ bool ”},
"created_at": { "$ref": "../base/definitions.schema.json#/definitions/datetime"},
“名称”:{“$ref”:“../base/base/definitions.schema.json#/definitions/name”},
“updated_at”:{“$ref”:“../base/definitions.schema.json#/definitions/datetime”}
},
“必需”:[“名称”],
“类型”:“对象”
}
目录结构:
api
- 根据
-- 定义.schema.json
- 国家
--国家.schema.json
我通过使用绝对路径尝试了几种组合,file
url 和其他几种路径组合。不知道发生了什么。
模式验证器:ajv@5.1.1
最佳答案
您需要使用“addSchema”方法添加模式。 $ref 是相对于“id”属性(draft-06 中的“$id”)解析的,ajv 不(也不能)使用文件路径。
编辑:将 $ref 部分添加到 docs .
关于jsonschema - 如何在 JSON Schema 中使用来自外部文件的定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46388284/