Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining an array as non empty in json schema

Tags:

json

arrays

Is there any way to define an array as non empty in JSON schema?

like image 897
sudha Avatar asked Apr 19 '17 08:04

sudha


People also ask

How do you declare an array in JSON Schema?

To do this, we use the prefixItems keyword. prefixItems is an array, where each item is a schema that corresponds to each index of the document's array. That is, an array where the first element validates the first element of the input array, the second element validates the second element of the input array, etc.

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 allOf in JSON Schema?

allOf: (AND) Must be valid against all of the subschemas. anyOf: (OR) Must be valid against any of the subschemas. oneOf: (XOR) Must be valid against exactly one of the subschemas.

Can JSON array have different types?

JSON allows entries in JSON arrays to have different structure and types. For JSON Objects, some JSON applications may also allow the same name for contained objects or values which may then have different structure and types.


1 Answers

As you can see in the documentation, you can define the minimum amount of items with "minItems": 1:

{
    "id": "http://some.site.somewhere/entry-schema#",
    "$schema": "http://json-schema.org/draft-04/schema#",
    "description": "schema for an fstab entry",
    "type": "object",
    "properties": {
        "options": {
            "type": "array",
            "minItems": 1, // -> array must not be empty
            "items": { "type": "string" },
            "uniqueItems": true
        },
        "readonly": { "type": "boolean" }
    }
}
like image 180
Matthias Burger Avatar answered Sep 22 '22 04:09

Matthias Burger