2015-02-24 14 views
7

Ich habe ein Objekt, in dem der "Schlüssel" der Eigenschaft dynamisch festgelegt wird ... Was ist die richtige Art, dies in einem JSON-Schema zu definieren? DieseJSON-Schema für dynamische Eigenschaften

ist, was mein Objekt sieht aus wie

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

Der „Schlüssel“ der „column_definitions“ wird immer dynamisch sein (es „Field_1“ so viel nur sein kann, wie es sein kann „Field_24“

Was ist die richtige dies in JSON Schema definieren

ich möchte nicht nur sagen, „type“: „Objekt“, weil ich die statischen Eigenschaften „Typ“ und „isNullable“ zu definieren auch in der Lage sein wollen , ich kann "oneOf" einfach nicht benutzen, weil ich nicht weiß, was th Der "Schlüssel" kann potentiell sein und es gibt keine festgelegten Potentialwerte.

Dies ist, was ich bisher:

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

Können Sie es lösen? Ich habe jetzt dieselbe Situation – mnvbrtn

Antwort

8

Ich denke, was Sie suchen das patternProperties Feld, anstatt die properties ein. Sollte in etwa so aussehen, vorausgesetzt, Sie wollen nur eine Übereinstimmung aller Muster:

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