Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the min size of array in the json schema

I want to make a schema of json file.It's for an array of products.

The json schema is similar as below:

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

The array should at least one item in it. How can I define the minimum of the array?

Do I need to add the minimun defination?

like image 990
Stella Avatar asked May 16 '13 09:05

Stella


People also ask

Which keyword helps define minimum length of string in JSON Schema?

The pattern keyword is used to restrict a string to a particular regular expression.

What is JSON Schema additionalItems?

As of 2019-09 json-schema specification version, additionalProperties is a validation keyword for objects/documents, and additionalItems is a validation keyword for arrays. Follow this answer to receive notifications.

How are arrays represented in JSON?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.

What is a schema array?

Array Schemas Arrays are used to represent ordered sets of values, such as the following sequence of strings: ["Chilean", "Argentinean", "Peruvian", "Colombian"] In this section we specify array's main charasteristics and restrictions that may apply to them using a single JSON Schema document.


1 Answers

To set the minimum # of item in an array, use the "minItems".

See:

https://datatracker.ietf.org/doc/html/draft-fge-json-schema-validation-00#section-5.3.3

and

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"]   } 
like image 172
damorin Avatar answered Sep 18 '22 14:09

damorin