如何将json模式分解为单独的文件?

如何将json模式分解为单独的文件?,json,jsonschema,Json,Jsonschema,从开始工作时,我希望将模式分解为更小的单独模式文件。这可能吗?如果可能,我如何引用架构文件的相对路径 baseSchema如下所示 { "id": "http://some.site.somewhere/entry-schema#", "$schema": "http://json-schema.org/draft-04/schema#", "description": "schema for an fstab entry", "type": "object",

从开始工作时,我希望将模式分解为更小的单独模式文件。这可能吗?如果可能,我如何引用架构文件的相对路径

baseSchema如下所示

{
    "id": "http://some.site.somewhere/entry-schema#",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "schema for an fstab entry",
    "type": "object",
    "required": [ "storage" ],
    "properties": {
        "storage": {
            "type": "object",
            "oneOf": [
                { "$ref": "#/definitions/diskDevice" },
                { "$ref": "#/definitions/diskUUID" },
                { "$ref": "#/definitions/nfs" },
                { "$ref": "#/definitions/tmpfs" }
            ]
        },
        "fstype": {
            "enum": [ "ext3", "ext4", "btrfs" ]
        },
        "options": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "string" },
            "uniqueItems": true
        },
        "readonly": { "type": "boolean" }
    },
    "definitions": {
        "diskDevice": {},
        "diskUUID": {},
        "nfs": {},
        "tmpfs": {}
    }
}
定义如下

磁盘设备

{
    "properties": {
        "type": { "enum": [ "disk" ] },
        "device": {
            "type": "string",
            "pattern": "^/dev/[^/]+(/[^/]+)*$"
        }
    },
    "required": [ "type", "device" ],
    "additionalProperties": false
}
diskUUID

{
    "properties": {
        "type": { "enum": [ "disk" ] },
        "label": {
            "type": "string",
            "pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$"
        }
    },
    "required": [ "type", "label" ],
    "additionalProperties": false
}
nfs

tmpfs

{
    "properties": {
        "type": { "enum": [ "tmpfs" ] },
        "sizeInMB": {
            "type": "integer",
            "minimum": 16,
            "maximum": 512
        }
    },
    "required": [ "type", "sizeInMB" ],
    "additionalProperties": false
}
我的目录结构如下所示

{
    "id": "http://some.site.somewhere/entry-schema#",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "schema for an fstab entry",
    "type": "object",
    "required": [ "storage" ],
    "properties": {
        "storage": {
            "type": "object",
            "oneOf": [
                { "$ref": "#/definitions/diskDevice" },
                { "$ref": "#/definitions/diskUUID" },
                { "$ref": "#/definitions/nfs" },
                { "$ref": "#/definitions/tmpfs" }
            ]
        },
        "fstype": {
            "enum": [ "ext3", "ext4", "btrfs" ]
        },
        "options": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "string" },
            "uniqueItems": true
        },
        "readonly": { "type": "boolean" }
    },
    "definitions": {
        "diskDevice": {},
        "diskUUID": {},
        "nfs": {},
        "tmpfs": {}
    }
}


因此,我不想将diskDevice、diskUUID、nfs和Tempf放在根架构的“定义”中,而是将它们作为单独的架构放在各自的文件中。

要将它们分解为单独的文件,需要更改引用并为每个定义提供
id
。以
diskDevice.json
为例:

baseSchema.json 我将更改
id
以匹配文件名,因为它使id和文件之间的关系更加清晰

您还需要将引用从模式中的JSON指针更改为要引用的模式的ID。注意,ID解析类似于相对URL。这就是
diskDevice.json
解析为
http://some.site.somewhere/diskDevice.json#

diskDevice.json 您需要给出单独的模式ID。同样,我会使用与文件名匹配的ID,以使事情更清楚。(还应添加
$schema
类型

使用模式
如何处理这些模式将取决于验证器。使用我使用的验证器()加载所有模式,并使用它们的ID解析它们。

基本URL是否表示基本模式文件的路径?我没有在线存储架构定义,它们将存储在本地磁盘上。否。不一定。它只是一种名称空间机制——或者至少可以作为一种机制使用。我的文件存储为文件,可以说是手动加载到ajv中。ajv然后在它们全部加载后通过I'd解决它们。不同的验证器可能会以不同的方式运行,但您肯定不需要为模式提供服务。那么它如何知道在哪里查找文件呢?例如,如果其中一个架构文件驻留在子文件夹中,该怎么办?这看起来怎么样?您可能希望将其作为一个单独的、特定于验证器的问题发布。此答案详细说明了模式ID和引用如何解析单独的模式。如何将它们加载到验证器中在很大程度上取决于所讨论的验证器。使用ajv,我加载文件,但它找不到它们。
{
    "id": "http://some.site.somewhere/baseSchema.json#",
    ...
    "properties": {
        "storage": {
            "type": "object",
            "oneOf": [
                { "$ref": "diskDevice.json#" },
                { "$ref": "diskUUID.json#" },
                { "$ref": "nfs.json#" },
                { "$ref": "tmpfs.json#" }
            ]
        },
        ...
    }
}
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://some.site.somewhere/diskDevice.json#",
    "type": "object",
    ...
}