Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON schema for dynamic properties

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 }
          }
        }              
      }
    }
  }
}
like image 566
Gustavo Avatar asked Feb 24 '15 13:02

Gustavo


People also ask

What is JSON Schema properties?

Properties. The properties (key-value pairs) on an object are defined using the properties keyword. The value of properties is an object, where each key is the name of a property and each value is a schema used to validate that property.

What does $Ref mean in JSON Schema?

In a JSON schema, a $ref keyword is a JSON Pointer to a schema, or a type or property in a schema. A JSON pointer takes the form of A # B in which: A is the relative path from the current schema to a target schema. If A is empty, the reference is to a type or property in the same schema, an in-schema reference.

What is anyOf in JSON Schema?

react-jsonschema-form supports custom widgets for oneOf, anyOf, and allOf. A schema with oneOf is valid if exactly one of the subschemas is valid. A schema with anyOf is valid if at least one of the subschemas is valid. A schema with allOf is valid if all of the subschemas are valid.

What is oneOf in JSON Schema?

Here oneOf is a keyword construct in the JSON Schema, which is used to provide an array of criteria where, if exactly one of them is valid, the whole block is valid. As per the exampe above, objects having ( "email" AND "password" ) OR ( "username" AND "password" ) attributes are considered valid.


1 Answers

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
                        }
                    }
                }
            }
        }
    }
}
like image 89
fabianvf Avatar answered Sep 20 '22 14:09

fabianvf