2013-05-16 4 views
29

Ich möchte ein Schema der JSON-Datei.Es ist für ein Array von Produkten.Wie definiert man die minimale Größe von Array in der JSON-Schema

Das json Schema ist ähnlich wie im folgenden:

{ 
"$schema": "http://json-schema.org/draft-04/schema#", 
"title": "Product set", 
"type": "array", 
"items": { 
    "title": "Product", 
    "type": "object", 
    "properties": { 
     "id": { 
      "description": "The unique identifier for a product", 
      "type": "number" 
     }, 
     "name": { 
      "type": "string" 
     }, 
     "price": { 
      "type": "number", 
      "minimum": 0, 
      "exclusiveMinimum": true 
     }, 
     "tags": { 
      "type": "array", 
      "items": { 
       "type": "string" 
      }, 
      "minItems": 1, 
      "uniqueItems": true 
     }, 
     "dimensions": { 
      "type": "object", 
      "properties": { 
       "length": {"type": "number"}, 
       "width": {"type": "number"}, 
       "height": {"type": "number"} 
      }, 
      "required": ["length", "width", "height"] 
     }, 
     "warehouseLocation": { 
      "description": "Coordinates of the warehouse with the product", 
      "$ref": "http://json-schema.org/geo" 
     } 
    }, 
    "required": ["id", "name", "price"] 
} 
} 

Die Anordnung sollte mindestens ein Element darin. Wie kann ich das Minimum des Arrays definieren?

Muss ich die minimale Definition hinzufügen?

Antwort

7

Ich nehme an, nein, zumindest auf Arbeitsentwurf der minimum wird nur für numerische Werte, nicht Arrays.

5.1. Validation Schlüsselwörter für numerische Instanzen (Anzahl und integer)
...
5.1.3. minimum and exclusiveMinimum

So sollten Sie mit min/maxitems für Arrays gut sein.

4

Es sieht so aus, als ob Draft V4 erlaubt, was Sie suchen. Von http://json-schema.org/example1.html:

{ 
"$schema": "http://json-schema.org/draft-04/schema#", 
"title": "Product", 
"description": "A product from Acme's catalog", 
"type": "object", 
"properties": { 
    ... 
    "tags": { 
     "type": "array", 
     "items": { 
      "type": "string" 
     }, 
     "minItems": 1, 
     "uniqueItems": true 
    } 
}, 
"required": ["id", "name", "price"] 
} 

Beachten Sie, dass die "Tags" Eigenschaft als Array definiert ist, mit einer minimalen Anzahl von Punkten (1).

38

Verwenden Sie die "minItems", um die Mindestanzahl von Elementen in einem Array festzulegen.

Siehe:

http://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.3.3

und

http://jsonary.com/documentation/json-schema/?section=keywords/Array%20validation

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "title": "Product", 
    "description": "A product from Acme's catalog", 
    "type": "object", 
    "properties": { 
     ... 
     "tags": { 
      "type": "array", 
      "items": { 
       "type": "string" 
      }, 
      "minItems": 1, 
      "maxItems": 4, 
      "uniqueItems": true 
     } 
    }, 
    "required": ["id", "name", "price"] 
    } 
+1

ein Beispiel ist nützlicher als Links (die im Laufe der Zeit verrotten können) –

0

Wenn Sie ein leeres Array erwarten, es wie folgt definieren.

{ 
    "items": [ {} ], 
    "additionalItems": false 
} 
Verwandte Themen