我正在尝试使用来自 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": { ... } }