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
fstab
schema - Starting the
entry
schema - Constraining an entry
- The
diskDevice
definition - The
diskUUID
definition - The
nfs
definition - The
tmpfs
definition - The full entry schema
- Referencing the
entry
schema in thefstab
schema
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
$id
keyword. - The
$schema
keyword. - The
type
validation keyword. - The
required
validation keyword. - The
properties
validation keyword.- The
/
key is empty now; We will fill it out later.
- The
- The
patternProperties
validation 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
additionalProperties
validation keyword.- The value here is
false
to 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
description
annotation keyword. - The
oneOf
keyword. - The
$ref
keyword.- 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
$defs
keyword.- 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
fstype
key uses theenum
validation keyword. - Our
options
key uses the following:- The
type
validation keyword (see above). - The
minItems
validation keyword. - The
items
validation keyword. - The
uniqueItems
validation keyword. - Together these say:
options
must 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
readonly
key.
With these added constraints, the schema now looks like this:
The diskDevice
definition
One new keyword is introduced here:
- The
pattern
validation keyword notes thedevice
key 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
format
annotation and assertion keyword.
The tmpfs
definition
Our last definition introduces two new keywords:
- The
minimum
validation keyword. - The
maximum
validation 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!.