Combining Subschemas
In this module, you will learn how to combine multiple subschemas to create more complex JSON Schemas. You will learn about the following keywords: allOf, anyOf, oneOf, not, $defs,$ref and recursive schemas.
$defs and $ref
In JSON Schema, the $defs keyword allows you to define reusable subschemas. You can then reference these subschemas using the $ref keyword.
To define a subschema with $defs, you can use the following syntax:
1{ 2 "$defs": { 3 "mySubschema": { 4 "type": "string", 5 "maxLength": 10 6 } 7 } 8}
In the above example, we define a subschema named mySubschema which specifies that the value should be a string with a maximum length of 10 characters.
To reference this subschema using $ref, you can use the following syntax:
1{ 2 "$ref": "#/$defs/mySubschema" 3}
In the above example, we reference the mySubschema subschema using $ref. This allows us to reuse the definition of mySubschema wherever we need it.
Using $defs and $ref can help make your JSON Schema more modular and maintainable by promoting code reuse.
Task
1{ 2 "name":{ 3 "firstName":"John", 4 "middleName":"Smith", 5 "lastName":"Doe" 6 } 7}
Define a subschema named stringType in the schema given in side editor, which is just "type": "string" and then reference it using $ref for the properties firstName, middleName and lastName