json - 如何管理多个 JSON 模式文件?

标签 json node.js schema jsonschema

我正在尝试使用来自 commonjs-utils 的 node.js + json-schema.js 验证我的 JSON API。单次验证很容易,但找不到正确的方法来管理多个架构文件以实现相互引用。

假设我有两个模型和两个 API。

// book
{
  "type": "object",
  "properties": {
      "title": { "type": "string" },
      "author": { "type": "string" }
  }
}
// author
{
  "type": "object",
  "properties": {
      "first_name": { "type": "string" },
      "last_name": { "type": "string" }
  }
}  
// authors API
{
  "type": "array",
  "items": { "$ref": "author" }
}
// books API: list of books written by same author
{
  "type": "object",
  "properties": {
    "author": { "$ref": "author" } 
    "books": { "type": "array", "items": { "$ref": "book" } }
  }
}  

每个架构都应该分成单独的文件并在线吗?或者我可以像下面这样组合成单个模式文件吗?如果可能,我如何引用本地架构?

// single schema file {
    "book": { ... },
    "author": { ... },
    "authors": { ... },
    "books": { ... } }

最佳答案

在 JSON 模式中,您可以为每个文件放置一个模式,然后使用它们的 URL(存储它们的位置)或带有 id 标记的大模式访问它们。

这是一个大文件:

{
    "id": "#root",
    "properties": {
        "author": {
            "id": "#author",
            "properties": {
                "first_name": {
                    "type": "string"
                },
                "last_name": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        // author
        "author_api": {
            "id": "#author_api",
            "items": {
                "$ref": "author"
            },
            "type": "array"
        },
        // authors API
        "book": {
            "id": "#book",
            "properties": {
                "author": {
                    "type": "string"
                },
                "title": {
                    "type": "string"
                }
            },
            "type": "object"
        },
        // books API: list of books written by same author
        "books_api": {
            "id": "#books_api",
            "properties": {
                "author": {
                    "$ref": "author"
                },
                "books": {
                    "items": {
                        "$ref": "book"
                    },
                    "type": "array"
                }
            },
            "type": "object"
        }
    }
}

然后您可以将您的验证器引用到这些子架构之一(使用 id 定义)。

在你的架构之外,这个:

{ "$ref": "url://to/your/schema#root/properties/book" }

等价于:

{ "$ref": "url://to/your/schema#book" }

…从内部来说,这相当于:

{ "$ref": "#root/properties/book" }

或者这个(仍然来自内部):

{ "$ref": "#book" }

见我的answer here了解更多信息。

关于json - 如何管理多个 JSON 模式文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8179137/

相关文章:

node.js - 零停机升级nodejs websocket Web应用程序

php - Symfony 和 JMSSerialier,无法添加监听器来添加额外字段

json - Golang 序列化/反序列化一个空数组而不是 null

json - 使用 Jersey 和 Jackson 处理 JSON 请求

node.js - Sequelize : Can't do update on column UpdatedAt

javascript - 如何在控制台应用程序中实现日志记录?

javascript - 从页面回调获取JSON数据

xml - 无法将名称 X 解析为递归 xml 架构中的元素声明组件

mongodb - 在 Mongoose 中存储时间的最佳方式

schema - 如何获取当前的 Datomic 架构?