Ensuring Array Content with the 'contains'
The contains keyword is used to ensure that an array contains at least one element that matches the subschema defined in the contains keyword.
Example
In the following example, we have a JSON Schema that validates an array of numbers. The schema ensures that the array contains at least one element that is less than 10.
1{ 2 "type": "array", 3 "contains": { 4 "type": "number", 5 "maximum": 10 6 } 7}
Task
Our employee document has a field called skills.
1{ 2 "name": "John Doe", 3 "age": 30, 4 "skills": ["JavaScript", "Python", "Java"] 5}
Now, we want to ensure that the skills array contains at least one element that is equal to "JavaScript". modify the schema given to you in the side editor to apply this constrain.
Hint: Use the const keyword to specify JavaScript as the required element.
Loading...
Output
Please click the button or use
Shift+Enter
to view the output