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

ero*_*las 10 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",
    "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": {}
    }
}
Run Code Online (Sandbox Code Playgroud)

定义就是这些

磁盘设备

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

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
}
Run Code Online (Sandbox Code Playgroud)

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
}
Run Code Online (Sandbox Code Playgroud)

TMPFS

{
    "properties": {
        "type": { "enum": [ "tmpfs" ] },
        "sizeInMB": {
            "type": "integer",
            "minimum": 16,
            "maximum": 512
        }
    },
    "required": [ "type", "sizeInMB" ],
    "additionalProperties": false
}
Run Code Online (Sandbox Code Playgroud)

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

在此输入图像描述

所以我不想将diskDevice,diskUUID,nfs,tempfs放在根模式的"定义"中,而是将它们作为单独的模式放在各自的文件中.

car*_*ant 5

要将它们分解为单独的文件,您需要更改引用并为每个定义提供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#" }
            ]
        },
        ...
    }
}
Run Code Online (Sandbox Code Playgroud)

我将更id改为与文件名匹配的名称,因为这会使id和文件之间的关系更加清晰。

您还需要将引用从架构中的JSON指针更改为要引用的架构的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",
    ...
}
Run Code Online (Sandbox Code Playgroud)

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

使用架构

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