【问题标题】:JSON schema for dynamic properties动态属性的 JSON 模式
【发布时间】:2015-04-26 03:49:34
【问题描述】:

我有一个对象,其中属性的“键”将被动态设置...在 JSON 模式中定义它的正确方法是什么?

这就是我的对象的样子

{
  "column_definitions": [    
    {
     "Field_1": {
       "type": "Numeric",
       "isNullable": false
      }
    },
    {
     "Field_2": {
       "type": "Boolean",
       "isNullable": true
      }
    }
 ],
 "row_values": [ ... ]
}

“column_definitions”的“key”总是动态的(它可以是“Field_1”,也可以是“Field_24”

在 JSON Schema 中定义它的正确方法是什么?

我不想只说“类型”:“对象”,因为我希望能够定义静态属性“类型”和“isNullable” 另外,我不能使用“oneOf”,因为我不知道“关键”可能是什么,并且没有设定的潜在值。

这是我目前所拥有的:

{
  "$schema": "http://json-schema.org/draft-04/schema",
  "title": "SomeSchema",
  "description": "SomeDescription",
  "type": "object",
  "properties": 
  {
    "column_definitions": { "type": ["array", "null"], "items": { "$ref": "#/definitions/columnDef" }, "readOnly": true },
    "row_values": { "type": ["array", "null"], "items": { "type": "object" }, "readOnly": true }
  },
  "definitions": {
    "columnDef" : {
      "type": "object",
      "properties": {
        "THIS_IS_MY_DYNAMIC_PROPERTY": {
          "type": "object",
          "properties": {
            "type": { "type" : ["string", "null"], "enum": ["Text", "Boolean", "Numeric", "DateTime"], "readOnly": true },
            "isNullable": { "type" : ["boolean", "null"], "readOnly": true }
          }
        }              
      }
    }
  }
}

【问题讨论】:

  • 你能解决吗?我现在也面临同样的情况

标签: json properties schema jsonschema


【解决方案1】:

我认为您正在寻找的是 patternProperties 字段,而不是 properties 字段。应该看起来像这样,假设您只想要一个匹配所有模式:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "title": "SomeSchema",
    "description": "SomeDescription",
    "type": "object",
    "properties": {
        "column_definitions": {
            "type": [
                "array",
                "null"
            ],
            "items": {
                "$ref": "#/definitions/columnDef"
            },
            "readOnly": true
        },
        "row_values": {
            "type": [
                "array",
                "null"
            ],
            "items": {
                "type": "object"
            },
            "readOnly": true
        }
    },
    "definitions": {
        "columnDef": {
            "type": "object",
            "patternProperties": {
                ".*": {
                    "type": "object",
                    "properties": {
                        "type": {
                            "type": [
                                "string",
                                "null"
                            ],
                            "enum": [
                                "Text",
                                "Boolean",
                                "Numeric",
                                "DateTime"
                            ],
                            "readOnly": true
                        },
                        "isNullable": {
                            "type": [
                                "boolean",
                                "null"
                            ],
                            "readOnly": true
                        }
                    }
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2015-10-20
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 2012-11-02
    • 2021-10-26
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多