Arrays
Welcome, to the Arrays module! In this module, you will learn how to use JSON Schema to validate arrays, apply constraints on the array items, and handle additional items in an array.
Specifying Length of an Array
Consider the employee JSON Document, now has a new field: an array of phone numbers.
1{ 2 "name": "John Doe", 3 "age": 25, 4 "phones": ["123-456-7890", "987-654-3210"] 5}
Schema for the Array
In JSON Schema, you can specify the minimum and maximum number of items in an array using the minItems and maxItems keywords.
Example
1 { 2 "hobbies": { 3 "type": "array", 4 "items": {...} 5 "minItems": 1, 6 "maxItems": 5 7 } 8 }
Now, modify the phones property of the given schema on the side editor, and specify minimum items as 1 and maximum items as 3. Additionally constrain the phone numbers to be in the format xxx-xxx-xxxx.
Hint: Use regular expression ^\\d{3}-\\d{3}-\\d{4}$ with pattern keyword to validate the phone number format.
Loading...
Output
Please click the button or use
Shift+Enter
to view the output