New2025 wrap-up: Voyage AI, AMP launch, & customer wins. Plus, 2026 predictions. Read blog >
NewBuild better RAG. Voyage 4 models & Reranking API are now on Atlas. Read blog >
NewIntroducing Automated Embedding: One-click vector search, no external models. Read blog >
Blog home
arrow-left

JSON Schema Validation - Checking Your Arrays

May 9, 2019 | Updated: November 18, 2019

In a previous post, I discussed some of the methods that can be used to “lock down” the schema in your MongoDB documents. In this second part of this series, I’ll continue on with techniques beyond simple required field and value validation. We’ll explore how to further benefit from MongoDB’s document model and see how to apply another validation technique to arrays.

Checking Your Arrays

We dealt with checking an array's structural properties in the last post, but this time we want to deal with an aspect of the data they contain. An array holds a selection of items. Sometimes, well usually, these are all different, but sometimes there may be duplicates of the same item. If we want to ensure that they are all different, then we need to ensure the contents of the array are unique within the array.

Imagine if your culinary endeavors take you to a food coloring company. Your product is selling sets of a variety of colors of food coloring. It would probably be a great idea to make sure that each box of food coloring includes different colors. How exciting would it be for a customer to only get a box of blue food coloring? We can use the uniqueItems keyword in our schema validator to ensure uniqueness.

Our packages of food coloring, as defined above, must contain unique containers of dyes. That uniqueness is judged by the combination of size and color.

Document 1

We can of course have a box with three different sizes of different colors.

Each item is unique. So this works.

Document 2

We could have a package with three different sizes of the same color, but the combination of color and package size must be unique in the dyes array. That means that when a document is inserted like this:

It is valid because each item in the array is a unique color and size combination. Let’s try another one:

Document 3

Let's fill a box with red coloring in various sizes:

We see here that due to there being two small red containers in this package, the insert fails.

Document 4

What if someone tries to create a "special edition" of coloring with an aroma or taste to make the boxes more interesting? The validation schema helps there too.

Being able to validate schema shape and values is a valuable and powerful tool. We can extend our schema validation process by defining a schema based on specific properties.

Conclusion

JSON schema validation can greatly enhance your application and add security to your system. In this particular case, we've used validation to ensure that there are no duplicates in embedded arrays in a document, entirely through defining the schema and with no additional code. We've also protected against having unauthorized extensions to the specification of array objects. For our food coloring example, the validation schema can't solve the variety problem, but it can prevent some of the worst failures possible at a database level.

The techniques provided both here and in the previous post, are wonderful tools to have in your toolbox when working with the MongoDB document model. In part three of this series, I’ll explore schema dependencies and show how to make fields dependent on the existence of others.

JSON Schema Validation Series

MongoDB Resources
Atlas Learning Hub|Customer Case Studies|AI Learning Hub|Documentation|MongoDB University