JSON schema for dynamic properties

12,207

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
                        }
                    }
                }
            }
        }
    }
}
Share:
12,207

Related videos on Youtube

Gustavo
Author by

Gustavo

Updated on September 15, 2022

Comments

  • Gustavo
    Gustavo over 1 year

    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 }
              }
            }              
          }
        }
      }
    }