Getting Started
Modeling a file system with JSON Schema
In this step-by-step guide you will learn how to design a JSON Schema that mirrors the structure of an /etc/fstab file.
This guide is divided into the following sections:
- Introduction
- Creating the
fstabschema - Starting the
entryschema - Constraining an entry
- The
diskDevicedefinition - The
diskUUIDdefinition - The
nfsdefinition - The
tmpfsdefinition - The full entry schema
- Referencing the
entryschema in thefstabschema
Introduction
Not all constraints to an fstab file can be modeled using JSON Schema alone; however, it can represent a good number of them and the exercise is useful to demonstrate how constraints work. The examples provided are illustrative of the JSON Schema concepts rather than a real, working schema for an fstab file.
This example shows a possible JSON Schema representation of file system mount points as represented in an /etc/fstab file.
An entry in an fstab file can have many different forms; Here is an example:
Creating the fstab schema
We will start with a base JSON Schema expressing the following constraints:
- the list of entries is a JSON object;
- the member names (or property names) of this object must all be valid, absolute paths;
- there must be an entry for the root filesystem (ie,
/).
Building out our JSON Schema from top to bottom:
- The
$idkeyword. - The
$schemakeyword. - The
typevalidation keyword. - The
requiredvalidation keyword. - The
propertiesvalidation keyword.- The
/key is empty now; We will fill it out later.
- The
- The
patternPropertiesvalidation keyword.- This matches other property names via a regular expression. Note: it does not match
/. - The
^(/[^/]+)+$key is empty now; We will fill it out later.
- This matches other property names via a regular expression. Note: it does not match
- The
additionalPropertiesvalidation keyword.- The value here is
falseto constrain object properties to be either/or to match the regular expression.
- The value here is
You will notice that the regular expression is explicitly anchored (with ^ and $): in JSON Schema, regular expressions (in patternProperties and in pattern) are not anchored by default.
Starting the entry schema
We will start with an outline of the JSON schema which adds new concepts to what we've already demonstrated.
We saw these keywords in the prior exercise: $id, $schema, type, required and properties.
To this we add:
- The
descriptionannotation keyword. - The
oneOfkeyword. - The
$refkeyword.- In this case, all references used are local to the schema using a relative fragment URI (
#/...).
- In this case, all references used are local to the schema using a relative fragment URI (
- The
$defskeyword.- Including several key names which we will define later.
Constraining an entry
Let's now extend this skeleton to add constraints to some of the properties.
- Our
fstypekey uses theenumvalidation keyword. - Our
optionskey uses the following:- The
typevalidation keyword (see above). - The
minItemsvalidation keyword. - The
itemsvalidation keyword. - The
uniqueItemsvalidation keyword. - Together these say:
optionsmust be an array, and the items therein must be strings, there must be at least one item, and all items should be unique.
- The
- We have a
readonlykey.
With these added constraints, the schema now looks like this:
The diskDevice definition
One new keyword is introduced here:
- The
patternvalidation keyword notes thedevicekey must be an absolute path starting with /dev.
The diskUUID definition
No new keywords are introduced here.
We do have a new key: label and the pattern validation keyword states it must be a valid UUID.
The nfs definition
We find another new keyword:
- The
formatannotation and assertion keyword.
The tmpfs definition
Our last definition introduces two new keywords:
- The
minimumvalidation keyword. - The
maximumvalidation keyword. - Together these require the size be between 16 and 512, inclusive.
The full entry schema
The resulting schema is quite large:
Referencing the entry schema in the fstab schema
Coming full circle we use the $ref keyword to add our entry schema into the keys left empty at the start of the exercise:
- The
/key. - The
^(/[^/]+)+$key.
Need Help?
Did you find these docs helpful?
Help us make our docs great!
At JSON Schema, we value docs contributions as much as every other type of contribution!
Still Need Help?
Learning JSON Schema is often confusing, but don't worry, we are here to help!.