Is there any way to define an array as non empty 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.
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.
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.
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.
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" }
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With