【问题标题】:Allow Properties to be any string允许属性为任何字符串
【发布时间】:2021-07-30 00:06:59
【问题描述】:

给定以下 json 文档:

{
    "property-1": {
        "server": "string-value",
        "environment": "string-value",
        "cluster": "string-value"
    },
    "property-2": {
        "server": "string-value",
        "environment": "string-value",
        "cluster": "string-value"
    }
}

我正忙着为这种类型的 json 文档创建一个 json 模式,其中property-1 可以是任何字符串,但在其中,我们将拥有一个包含许多必需的集合结构。我正在努力的是如何定义架构,以便用户能够为property-1 | property-2 设置任何字符串。

我认为我必须使用一种模式,但我尝试过的方法并没有那么好。

{
    "$schema": "http://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "definitions": {
        "role": {
            "$id": "#svcrole",
            "description": "Application name for which role is required.",
            "type": "object",
            "pattern": "^[a-zA-Z\\-]+$",
            "properties": {
                "server": {
                    "type": "string"
                },
                "environment": {
                    "type": "string"
                },
                "cluster": {
                    "type": "string"
                }
            },
            "required": [
                "server",
                "environment",
                "cluster"
            ]
        }
    },
    "properties": {
        "$schema": {
            "type": "string"
        },
        "$name": {
            "$ref": "#/definitions/role"
        }
    }
}

【问题讨论】:

    标签: json validation schema


    【解决方案1】:

    所以我在这里偶然发现了解决方案。

    您想使用propertyNamespatternProperties 喜欢:

    "propertyNames": {
        "pattern": "^[a-zA-Z\\-]+$"
    },
    "patternProperties": {
        "": {
            "$ref": "#/definitions/role"
        }
    }
    

    这将允许您使用模式来定义属性名称并使其全部动态化。

    【讨论】:

      猜你喜欢
      • 2021-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2020-01-04
      相关资源
      最近更新 更多