如何将json架构分解为单独的文件?

时间:2016-08-12 20:12:28

标签: json jsonschema

this example开始工作我想将架构分解为更小的单独架构文件。这是可能的,如果是这样,我如何引用模式文件的相对路径?

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": {}
    }
}

定义将是这些

diskDevice

{
    "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

{
    "properties": {
        "type": { "enum": [ "nfs" ] },
        "remotePath": {
            "type": "string",
            "pattern": "^(/[^/]+)+$"
        },
        "server": {
            "type": "string",
            "oneOf": [
                { "format": "host-name" },
                { "format": "ipv4" },
                { "format": "ipv6" }
            ]
        }
    },
    "required": [ "type", "server", "remotePath" ],
    "additionalProperties": false
}

tmpfs

{
    "properties": {
        "type": { "enum": [ "tmpfs" ] },
        "sizeInMB": {
            "type": "integer",
            "minimum": 16,
            "maximum": 512
        }
    },
    "required": [ "type", "sizeInMB" ],
    "additionalProperties": false
}

所以我的目录结构看起来像这样

enter image description here

所以我没有将diskDevice,diskUUID,nfs,tempfs放在根模式的“定义”中,而是希望将它们作为单独的模式放在各自的文件中。

1 个答案:

答案 0 :(得分:4)

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

baseSchema.json

{
    "id": "http://some.site.somewhere/baseSchema.json#",
    ...
    "properties": {
        "storage": {
            "type": "object",
            "oneOf": [
                { "$ref": "diskDevice.json#" },
                { "$ref": "diskUUID.json#" },
                { "$ref": "nfs.json#" },
                { "$ref": "tmpfs.json#" }
            ]
        },
        ...
    }
}

我会更改id以匹配文件名,因为它会使ID和文件之间的关系更加清晰。

您还需要将架构中的JSON指针的refs更改为要引用的架构的ID。请注意,ID会像相对URL一样解析。这是diskDevice.json#解析为http://some.site.somewhere/diskDevice.json#

diskDevice.json

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "id": "http://some.site.somewhere/diskDevice.json#",
    "type": "object",
    ...
}

您需要提供分离的模式ID。同样,我会使用与文件名匹配的ID - 以使事情清楚。 (您还应该添加$schematype。)

使用架构

您对这些架构的处理取决于您的验证器。使用我使用的验证器(ajv),我加载了所有模式,并使用它们的ID解析它们。