Writing a Layout

Until now we have generated a JSON Schema and JSON-LD context for the BlogPost type. We could use the same command to generate a JSON Schema and JSON-LD context for the User type. For instance, the generated JSON Schema would look like this:

{
	"type": "object",
	"properties": {
		"email": {
			"type": "string"
		},
		"name": {
			"type": "string"
		},
		"birthDate": {
			"type": "string",
			"format": "date"
		}
	},
	"required": [
		"email"
	]
}

However what if we want to avoid disclosing the birthdate of a user to the client? Or want to use a different name for the email property in the JSON Schema? We can do that with TreeLDR by defining a different layout for our User type.

Go back to the schema.tldr file and append the following layout definition at the end:

layout ClientUser for User {
	email as id,
	name
}

This defines a new layout for the User type, called ClientUser that will be used to communicate with the client applications. This layout contains all the properties of a user, except for the birth date, and the email property is renamed into id.

Use the tldrc compiler in your command line to generate a JSON Schema for this layout:

tldrc -i schema.tldr json-ld-context https://example.com/ClientUser

{
	"type": "object",
	"properties": {
		"id": {
			"type": "string"
		},
		"name": {
			"type": "string"
		}
	},
	"required": [
		"id"
	]
}

Last updated