So, let's construct the schema for our user profile object using JSON Schema. To do that, we must first understand its syntax.
The first thing to note is that a JSON Schema is itself a JSON object. Therefore, the simplest JSON Schema is simply an empty object:
{}
This empty object schema will allow any type of data, so it is pretty much useless. For it to be useful, we must describe the type of data we expect. This can be done through the type keyword. The type keyword expects its value to be either a string, where only one type is allowed, or an array, where any types specified in the array are allowed.
We expect our input for the user profile object to only be objects, and so we can specify a type of "object":
{ "type": "object" }
type is the most basic keyword. There are many other common keywords that are applicable to all types, such as title; there are also type-specific keywords, such as maximum, which only applies to data of type number.
For object types, we can use the type-specific keyword properties to describe what properties we expect our object to contain. The value of properties must be an object, with property names as the key and another valid JSON Schema, called a sub-schema, as the value. In our case, we expect the bio and summary properties to be of type string, and the name property to have an object type, so our schema would look like this:
{
"type": "object",
"properties": {
"bio": { "type": "string" },
"summary": { "type": "string" },
"name": { "type": "object" }
}
}