2013-05-16 34 views
29

Tôi muốn tạo một giản đồ tệp json. Nó là một mảng sản phẩm.Cách xác định kích thước tối thiểu của mảng trong lược đồ json

Giản đồ json là tương tự như sau:

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

Mảng nên ít nhất một mục trong đó. Làm thế nào tôi có thể xác định tối thiểu của mảng?

Tôi có cần thêm khử minimun không?

Trả lời

7

Tôi cho rằng không, ít nhất là tìm cách soạn thảo bản nháp minimum chỉ được áp dụng cho các giá trị số, không phải mảng.

5.1. từ khóa xác nhận cho trường hợp số (số lượng và số nguyên)
...
5.1.3. minimum and exclusiveMinimum

Vì vậy, bạn nên được tốt với min/maxItems cho mảng.

4

Dường như bản nháp v4 cho phép những gì bạn đang tìm kiếm. Từ 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"] 
} 

Lưu ý rằng thuộc tính "thẻ" được định nghĩa là mảng, với số lượng mặt hàng tối thiểu (1).

38

Để đặt số mục tối thiểu trong một mảng, hãy sử dụng "minItems".

Xem:

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

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

ví dụ hữu ích hơn liên kết (có thể bị thối theo thời gian) –

0

Nếu bạn đang mong đợi một mảng trống rỗng, định nghĩa nó như thế này.

{ 
    "items": [ {} ], 
    "additionalItems": false 
} 
Các vấn đề liên quan