Valid Against allOf the Subschemas (AND)
When you want to ensure that an instance is valid against all of the subschemas, you can use the allOf keyword.
You can think of allOf as an AND operation between subschemas. if one of the subschemas fails, the instance is considered invalid.
1{ 2 "name":"John Doe", 3}
We will create two subschemas 1. minStringLength and 2. alphaNumeric and then combine them using allOf in the name property.
1{ 2 "$defs": { 3 "minStringLength": {"minLength": 5}, 4 "alphaNumeric": {"pattern": "^[a-zA-Z0-9]*$"} 5 }, 6 "type": "object", 7 "properties": { 8 "name": { 9 "type": "string", 10 "allOf": [ 11 { "$ref": "#/$defs/minStringLength" }, 12 { "$ref": "#/$defs/alphaNumeric" } 13 ] 14 } 15 } 16}
The above schema ensures that the name property is valid against both the minStringLength and alphaNumeric subschemas. in other words, the name property must have a minimum length of 5 characters and must contain only alphanumeric characters.
Note: It is not necessary to define subschemas using $defs and $ref. You can directly define the subschemas inline as well.
Task
1{ 2 "name": "John Doe", 3 "age": 25 4}
For the given JSON document, you are provided with a schema in the side editor. the schema has defined a subschemas ageLimit. Your task is to:
- Define a inline subschemas that checks if the age is of type integer
- Combine the ageLimit and the inline subschemas using allOf in the age property.