Enumerated Array Items

Previously, We used enum to restrict the values of a string property. Similarly, you can restrict the values of array items using the enum keyword.

Let's add a new property relevantDepartments to the JSON document.

1{
2  "name": "John Doe",
3  "age": 25,
4  "relevantDepartments": ["HR", "Finance"]
5}

now, we want to restrict the relevantDepartments property to only a few specific values. You can use the enum keyword to define the allowed values.

Example

1{
2  "type": "array",
3  "items": {
4    "enum": ["HR", "Finance", "IT"]
5  }
6}

Now, Let's try to add the enum keyword to the schema given in the side editor and restrict the relevantDepartments property to only the values HR, Finance, IT, and Admin.

Also, each item in the array should be unique.

Remember: Items defined in the enum keyword are case-sensitive.

Hint: You can use the uniqueItems keyword to ensure that all items in the array are unique.

Loading...
Output
Please click the button or use
Shift+Enter
to view the output