【问题标题】:How do I break a json schema into separate files?如何将 json 模式分解为单独的文件?
【发布时间】:2016-08-12 20:12:28
【问题描述】:

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

定义将是这些

磁盘设备

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

磁盘UUID

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

所以我的目录结构是这样的

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

【问题讨论】:

    标签: json jsonschema


    【解决方案1】:

    要将它们分解为单独的文件,您需要更改引用并为每个定义提供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 指针的 ref 更改为您要引用的架构的 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 解析它们。

    【讨论】:

    • 基本 URL some.site.somewhere/baseSchema.json# 是否代表基本架构文件的路径?我没有在线存储架构定义,它们将存储在本地磁盘上。
    • 没有。不必要。它只是一种命名空间机制——或者至少可以作为一种机制使用。我的存储为文件并手动加载到 ajv 中,可以这么说。一旦它们全部加载,ajv 然后通过 Id 解决它们。不同的验证器可能会以不同的方式运行,但您当然不需要为模式提供服务。
    • 那么它怎么知道去哪里找文件呢?例如,如果其中一个架构文件位于子文件夹中怎么办?看起来怎么样?
    • 您可能希望将其作为一个单独的、特定于验证器的问题发布。这个答案详细说明了模式 id 和 refs 如何解决单独的模式。如何将它们加载到验证器中很大程度上取决于相关验证器。使用 ajv,我加载文件,它没有找到它们。
    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 2023-01-03
    • 2016-03-17
    • 2022-10-24
    相关资源
    最近更新 更多