Schema Validation in 3.7

Schema Validation in the upcoming ArangoDB 3.7

Disclaimer: Please note that this feature is currently available only in the Alpha 2 release of the upcoming ArangoDB 3.7. You can download the Alpha 2 preview here.

Being schema-less and allowing for flexible documents is one of big advantages of ArangoDB. But sometimes there are use-cases where we have a fixed schema. With the upcoming release 3.7, ArangoDB provides users with the means to validate the structure of documents using JSON Schema (draft-4).

The validation can be configured on collection level and with different strictness levels allowing users to import unclean data and improve later.

Let us consider a concrete example which you can directly try interactively below. Assume you have a collection containing customers and leads. For now you have only names and mail addresses for most of the customers similar as shown below:

Next, we will add validation for this collection. This is done by providing a well formed object as the one below to the validation attribute of the collection properties. Using arangosh this can be done by setting the db.customers.properties(<object>).

The object sets a message that will be displayed on validation failure. The level "moderate" allows us to work with old documents that are not conforming to the schema. Such documents can be updated but no new invalid objects can be added or objects that are valid can be changed to be invalid. The last attribute which is required is the rule attribute containing a valid JSON Schema description.

If we now try to insert a document where either the names or the mail is not a string the insert will fail with a validation failed message we specified earlier.

To make the validation more stringent you can change the validation level to strict and improve the schema. Then the validation object could look like:

By setting the level to "strict" all inserted or changed documents have to match the schema and the schema itself has become more rigorous as well. Now the names and mail have length requirements and are required to be present. Furthermore, there is an optional customer type property that must match one of the 3 given values. We do not allow additional properties to be added to the documents which is controlled by the additionalProperties false flag in the schema definition.

JSON Schema (draft-4) offers many more possibilities which you can explore here:

Do you like ArangoDB?
icon-githubStar this project on GitHub.
close-link