Skip to Content
Full-Stack React Projects - Second Edition
book

Full-Stack React Projects - Second Edition

by Shama Hoque
April 2020
Intermediate to advanced
716 pages
18h 55m
English
Packt Publishing
Content preview from Full-Stack React Projects - Second Edition

Validating array length in the game schema

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', ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Full-Stack React Projects

Full-Stack React Projects

Shama Hoque
React Native - The Practical Guide

React Native - The Practical Guide

Academind by Maximilian Schwarzmüller GmbH

Publisher Resources

ISBN: 9781839215414Supplemental Content