Better Validation with MongoDB Compass 1.18

In the most recent update to MongoDB Compass, the user experience of creating validation rules has been dramatically refreshed. This experience change also comes with support for JSON Schema validation.

MongoDB Compass is the official graphical client for MongoDB, offering the ability to deep dive into MongoDB databases and collections, create, edit, update, aggregate and delete data, perform index administration, view explain plans and turn your queries into code for applications.

Compass is available in a number of editions: a freely available community edition, a read-only edition and the full-featured version included with MongoDB Enterprise Advanced subscriptions. Validation is one of the features only available in the subscription version. In the most recent release, that validation has extended to include support for JSON Schema validation.

We're only going to touch on the mechanics of JSON Schema validation here. It's a powerful and standard way of defining the rules that are needed to ensure good quality documents. If you want to read more, then check out our recent series:

For now, we will continue looking at what's changed in Compass.

Validation with Compass

In earlier versions of Compass, a rules builder allowed MongoDB's own validation rules to be assembled in a controlled fashion. A JSON editor was available to edit the generated rules. You can read about this older experience in "Adding Document Validation Rules Using MongoDB Compass 1.5".

For Compass 1.18, the addition of a context-sensitive, field-aware editor has allowed for the removal of the rules builder to produce a more interactive and responsive validation creation environment. Let's have a look at it:

An Empty Rule

We've already logged in and connected with Compass and we're viewing one of the sample datasets available on MongoDB Atlas.

The display is now centered on a JSON editor, with controls for how the validation rules are to be handled above it. Below the editor is a new element, two windows for sample documents. One will display a document from the collection that passes our validation rule, the other will display a document that fails. As we update the rule in the editor, the two document windows will also update to give rapid feedback on the effect the rules will have when applied.

If we go into the editor and start typing, we'll also see some more of the features of the new Validation view:

Entering a $

As soon as we start typing, we are informed of errors in the rules and autocompletion kicks in offering up possible options. As the rules document is currently invalid, there's also nothing displayed in either of the sample document panels.

The editor is smart enough to know that this early in the rules document, $jsonSchema is a likely candidate to be what we want. Support for $jsonSchema is new in Compass 1.18 and we do want to use it so we'll let autocomplete do its thing.

Now we can enter a JSON Schema validation document. If you are new to the JSON Schema Validation, We're looking at the AirBnB sample data, so let's add a rule that entries must have a cleaning_fee field. Let's add required: [ "cleaning_fee" ] to the $jsonSchema.

A Required Rule and Results

There are no errors in our rules and the sample documents are back, complete with one document that passed and one that failed. These rules aren't in place yet though, this is only a preview. Notice the Update and Cancel buttons that appeared as soon as we started editing; clicking Update will activate the new validation rules on the database. Cancel will revert the rules document to how it was when we started editing.

Auto-completing - a closer look

We showed the editor's auto-completion earlier, but it does more than complete $ operators. For example, say we want to make a rule that the summary field must be more that 10 characters to be valid and we'll use the $jsonSchema format to do it. To start with we want to define properties in our schema:

Autocompleting prop

Well, we've got so far in, here you can see that the editor is offering a field called property_type which is one of the fields in the AirBnB dataset documents, and then offering properties which is a json-schema directive. You'll also notice the patternProperties match, where autocomplete is looking for shorthand matches. We'll cursor down and hit enter to select properties and move on:

Autocompleting summary

Now, we want to enter the field name and autocomplete kicks in quickly with just two field name options because it knows thats what is expected inside the properties object. We hit return to select summary and open up an object for the property settings.

Autocompleting type

Similarly, within the property settings, typing ty brings up just type from the json-schema keywords as this is the only thing that could appear in this position. When we go to enter the value, autocomplete offers options again:

Autocompleting a value

Given just the s, this time it draws from the type aliases and field names the autocomplete system knows; it's likely to be a type and that is what appears at the top of the list. We can finish up our validation settings for the summary field now, setting the minLength property to 10.

A Complete Validation Rule

And we will find that at least one document in the AirBnB sample dataset is failing the test.

Complete Validation

Compass 1.18's new Validation experience swaps rigid rule builders for a smart, helpful editor with rapid feedback, and in the process adds support for JSON Schema validation. We look forward to you enjoying the validation too.