Valid Against anyOf the Subschemas (OR)
anyOf keyword is used to define multiple subschemas. If the instance is valid against any one of the subschemas, the document is considered valid.
You can think of anyOf as an OR operation. If the instance is valid against any one of the subschemas, the document is considered valid.
Example
1{ 2 "name": "John Doe", 3 "age": 25, 4 "dateOfBirth": "1995-12-17" 5}
We want to allow both age and dateOfBirth properties in the document. The document is valid if it has either age or dateOfBirth property. we can use anyOf keyword to define this condition.
1{ 2 "type": "object", 3 "properties": {...}, 4 "anyOf": [ 5 {"required": ["age"]}, 6 {"required": ["dateOfBirth"]} 7 ] 8}
Task
Create a JSON schema that validates the following document.
1{ 2 "name": "John Doe", 3 "employeeType": "full-time", 4 "salary": 50000, 5 "hourlyRate": 25 6}
Modify the schema given to you in the side editor to ensure that the document is valid if:
- It has either salary or hourlyRate property.
- It has both salary and hourlyRate properties.
Loading...
Output
Please click the button or use
Shift+Enter
to view the output