April 2020
Intermediate to advanced
716 pages
18h 55m
English
In the game schema defining the Game model, we have two arrays for adding VR objects to the game. These answerObjects and wrongObjects arrays in a game document must contain at least one VR object in each array when a game is being saved in the game collection. To add this validation for a minimum array length to the game schema, we will add the following custom validation checks to the answerObjects and wrongObjects paths in the GameSchema defined with Mongoose.
We will use validate to add the array length validation for the answerObjects field, as shown in the following code:
mern-vrgame/server/models/game.model.js:
GameSchema.path('answerObjects').validate(function(v) { if (v.length == 0) { this.invalidate('answerObjects', ...