67

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?

Jyotir Bhandari
  • 109
  • 1
  • 10
Stella
  • 1,197
  • 1
  • 10
  • 18

3 Answers3

100

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"]
  }
Community
  • 1
  • 1
damorin
  • 1,389
  • 1
  • 12
  • 15
  • 5
    an example is more useful than links (which may rot over time) – Kate Gregory Apr 25 '14 at 19:26
  • 2
    It works as expected: when `"maxItems": 0`, validation fails if length of `"items"` array is a non-zero. Great! – klapshin Jan 30 '19 at 22:20
  • how can i add minLength / max Length fot every item (String) { "$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", "minLength": 1, "maxLength": 25 }, "minItems": 1, "maxItems": 4, "uniqueItems": true } }, "required": ["id", "name", "price"] } – Rio85 Nov 30 '21 at 13:40
  • You can add a defintion for the string that look like this (this one has a pattern for the size): "definitions" : { "Id": { "description" : "CktID description, can have a size 0 if used as end of list", "type":"string", "pattern" : "^([\u0020-\u007f]){0,28}$" }, – damorin Dec 01 '21 at 19:11
11

It looks like draft v4 permits what you are looking for. From 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"]
}

Notice that the "tags" property is defined as an array, with a minimum number of items (1).

U007D
  • 5,772
  • 2
  • 38
  • 44
9

I suppose no, at least looking to working draft the minimum is applied only for numeric values, not arrays.

5.1. Validation keywords for numeric instances (number and integer)
...
5.1.3. minimum and exclusiveMinimum

So you should be good with min/maxItems for arrays.

Community
  • 1
  • 1
dmi3y
  • 3,482
  • 2
  • 21
  • 32