如何在JSON模式中使用来自外部文件的定义? [英] How to use definitions from external files in JSON Schema?

查看:109
本文介绍了如何在JSON模式中使用来自外部文件的定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用$ref从另一个json模式导入定义,但出现以下错误:

I'm trying to import the definitions from another json schema using $ref but getting the following error:

can't resolve reference ../base/definitions.schema.json#/definitions/datetime from id #


{
  "$schema": "http://json-schema.org/draft-06/schema#",

  "definitions": {
    "datetime": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
  }
}


{
  "$schema": "http://json-schema.org/draft-06/schema#",
  "properties": {
    "active": {"type": "boolean"},
    "created_at": { "$ref": "../base/definitions.schema.json#/definitions/datetime" },
    "name": { "$ref": "../base/base/definitions.schema.json#/definitions/name" },
    "updated_at": { "$ref": "../base/definitions.schema.json#/definitions/datetime" }
  },
  "required": ["name"],
  "type": "object"
}

目录结构:


api
-- base
  -- definitions.schema.json
-- country
  -- country.schema.json

我通过使用绝对路径,file url和路径的其他几种组合尝试了几种组合.不知道发生了什么事.

I have tried several combinations by using an absolute path, a file url and several other combinations of the path. Not sure what's going on.

模式验证器:ajv@5.1.1

推荐答案

您需要使用"addSchema"方法添加架构. $ ref相对于"id"属性(在草案06中为"$ id")进行了解析,而ajv不(也不能)使用文件路径.

You need to add schemas using "addSchema" method. $ref is resolved relative to "id" attribute ("$id" in draft-06), ajv doesn't (and can't) use file paths.

将$ ref部分添加到文档.

added $ref section to docs.

这篇关于如何在JSON模式中使用来自外部文件的定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆