【问题标题】:Json Schema - Using enumerations using referenceJson Schema - 通过引用使用枚举
【发布时间】:2016-08-31 12:56:55
【问题描述】:

我正在尝试为我的用例构建一个JSON schema,在该用例中,我在一个单独的文件中有一个字符串的枚举,并希望从我的架构中引用它。我怎样才能做到这一点。

示例架构:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
        "card": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "integer"
                },
                "value": {
                    "type": "string",
                    "enum": {"$ref" : "reference to a file having list of enums"}
                  //I want to refer to a specific enum array (say value1's array)
                }
            }
        }
    },
    "required": [
        "card"
    ]
}

枚举文件如下:

{
"value1": [..],
"value2": [..]
....
}

【问题讨论】:

    标签: json jsonschema json-schema-validator


    【解决方案1】:

    $ref 只能用于引用模式。所以,你可以做这样的事情。

    {
        "$schema": "http://json-schema.org/draft-04/schema#",
        "type": "object",
        "properties": {
            "card": {
                "type": "object",
                "properties": {
                    "id": { "type": "integer" },
                    "value": { "$ref" : "/schemas/valueEnum.json" }
                }
            }
        },
        "required": ["card"]
    }
    

    /schemas/valueEnum.json

    { "enum": ["foo", "bar"] }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-30
      • 2012-02-14
      • 1970-01-01
      • 2023-02-06
      • 1970-01-01
      • 1970-01-01
      • 2018-03-18
      相关资源
      最近更新 更多