Schema Validator

Use Schema Validator to apply JSON Schema, the recommended way of performing MongoDB schema validation to a collection.

Add MongoDB schema validation

We’ll be applying schema to the Customers collection, which you can download here

  1. Right-click on your target connection (e.g. Customers)  in the Connection Tree.
  2. Choose Add/Edit Validator. This will open the Edit Validator window.Right-click on a collection and choose Add/Edit Validator

Paste JSON Schema validation rules

In the Edit Validator window, type in or paste your validation rules using JSON Schema.

Apply JSON schema validation rules to your collection

Example of JSON Schema

Below is the JSON Schema we applied to the Customers collection:

{ 
    "bsonType" : "object", 
    "required" : [
        "title", 
        "first", 
        "last", 
        "email", 
        "dob", 
        "address.street.name", 
        "address.street.suffix", 
        "address.street.number", 
        "address.city", 
        "address.state", 
        "address.zip-code", 
        "user_name", 
        "package", 
        "prio_support", 
        "registered_on", 
        "transactions", 
        "pet", 
        "number_pets", 
        "Studio_3T_edition", 
        "email"
    ], 
    "properties" : {
        "title" : {
            "bsonType" : "string", 
            "description" : "must be a string and is required"
        }, 
        "first" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "last" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "email" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "dob" : {
            "bsonType" : "date", 
            "description" : "must be a date and is not required"
        }, 
        "address.street.name" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "address.street.number" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "address.street.suffix" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "address.city" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "address.state" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "address.zip_code" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "user_name" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "package" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "prio_support" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "registered_on" : {
            "bsonType" : "date", 
            "description" : "must be a date and is not required"
        }, 
        "transactions" : {
            "bsonType" : "int32", 
            "description" : "must be an integer and is not required"
        }, 
        "pet" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }, 
        "number_pets" : {
            "bsonType" : "int32", 
            "description" : "must be an integer and is not required"
        }, 
        "Studio_3T_edition" : {
            "bsonType" : "string", 
            "description" : "must be a string and is not required"
        }
    }
}

Set validation levels

Set the validation to off, strict, or moderate directly in Studio 3T

You can choose how MongoDB applies your validation rules:

off
MongoDB disables validation completely
strict (default setting)
MongoDB applies validation rules to all inserts and updates
moderate
MongoDB applies validation rules to inserts and to updates to existing documents that already fulfill the validation criteria

Set validation action

Set the validation action to error or warn

MongoDB can handle documents that don’t meet the validation rules in two ways:

error (default setting)
Rejects inserts or updates that do not meet validation rules
warn
Allows inserts and updates while logging violations
Debugging validation error messages

The error messages don’t tell you what failed, even though MongoDB records the validation violations.

JSON schema validation error message

For further debugging, Studio 3T‘s Schema Explorer feature and other third-party JSON schema validation tools can come in handy, which we explain in detail in this tutorial.

Validate and save validation rules

Validate JSON before saving your MongoDB schema

Click on Validate to make sure the JSON is correct, then Save.

JSON Schema validation tutorial

Want to dive deeper into this topic? Read our tutorial, How to Create and Validate JSON Schema in MongoDB Collections, to learn more about how you can use SQL, Schema Explorer, and JSON Schema validation tools to help create your MongoDB schema.

Updated on November 23, 2018

Was this article helpful?

Related Articles