动态属性的JSON模式

Gus*_*avo 8 schema json properties jsonschema

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

这就是我的对象的样子

{
  "column_definitions": [    
    {
     "Field_1": {
       "type": "Numeric",
       "isNullable": false
      }
    },
    {
     "Field_2": {
       "type": "Boolean",
       "isNullable": true
      }
    }
 ],
 "row_values": [ ... ]
}
Run Code Online (Sandbox Code Playgroud)

"column_definitions"的"关键字"将始终是动态的(它可以是"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 }
          }
        }              
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

fab*_*nvf 14

我认为你所寻找的是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
                        }
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)