如何管理多个JSON模式文件? [英] How to manage multiple JSON schema files?

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

问题描述

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

I'm trying to validate my JSON API using node.js + json-schema.js from commonjs-utils. Just single validation was easy but could not find right way how to manage multiple schema files to enable referencing each other.

假设我有两个模型&两个API.

Suppose that I got two Models & two APIs.

// 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" } }
  }
}  

每个模式都应该划分为单独的文件并在线吗?还是可以合并为一个如下所示的单一架构文件?如果可能,如何引用本地架构?

Each schema should be divided in separate file and be online? Or Can I combine into single schema file like below? If it is possible, how can I reference local schema?

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

推荐答案

在JSON模式中,您可以为每个文件放置一个模式,然后使用它们的URL(存储它们的位置)访问它们,或者使用标签.

In JSON Schemas, you can either put a schema per file and then access them using their URL (where you stored them), or a big schema with id tags.

这里是一个大文件:

{
    "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定义).

You can then reference your validator to one of those sub schemas (which are defined with an id).

从架构之外,这是

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

等效于此:

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

…从内部等效于此:

… which is equivalent, from inside, to this:

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

或此(仍从内部):

{ "$ref": "#book" }

在此处查看我的在此处answer 了解更多信息.

See my answer here for more information.

这篇关于如何管理多个JSON模式文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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