JSON schema for dynamic properties

时光毁灭记忆、已成空白 提交于 2019-11-30 08:28:35

问题


i have an object in which the "key" of the property will be set dynamically... what is the right way of defining this in a JSON Schema?

This is what my object looks like

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

The "key" of the "column_definitions" will always be dynamic (it can be "Field_1" just as much as it can be "Field_24"

What is the proper to define this in JSON Schema?

I dont want to just say "type" : "object" because i want to be able to define the static properties "type" and "isNullable" Also, i cant use "oneOf" simply because i dont know what the "key" can potentially be and there is not a set potential values.

This is what i have so far:

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

回答1:


I think what you are looking for is the patternProperties field, rather than the properties one. Should look something like this, assuming you just want a match all pattern:

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


来源:https://stackoverflow.com/questions/28697209/json-schema-for-dynamic-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!