Mongoose doesn't validate nested array schema - javascript

I have the following schema:
const SectionSchema = new Schema({
title: {
type: String,
required: true,
maxlength: 24
},
message: {
type: String,
required: true,
minlength: 64,
maxlength: 1024
},
state: {
type: String,
validate: v => {
console.log("> section validation test");
return false;
}
}
}, {_id: false});
const InstructionSchema = new Schema({
name: {
type: String,
required: true
},
columns: [[SectionSchema]]
});
For the sake of reordering and simplicity I keep sections in a nested array of columns.
My problem is that no validation of SectionSchema is being performed, when I save changes:
// Create
const doc = new Instruction();
doc.name = "Test";
await doc.save(); // Save ok
// Later
const doc = await Instruction.findOne({name: "Test"});
doc.columns = [
[
{title: "Section1", message: "This is section one in column one.", state: "something"},
{title: "Section2", message: "This is section two in column one.", state: "something"},
],
[
{title: "Section1", message: "This is section one in column two."},
{title: "Section2"},
]
];
await doc.save(); // Save ok (doesn't validate?)
doc.columns[0][0].message = ""; // Should fail validation
await doc.save(); // Success...
When setting columns the save is successful, that means no validation is being made, including builtin and custom validation for each SectionSchema in the column array.
If my assumptions are correct, this is because for mongoose the columns field is a mixed type. Mongoose docs doesn't even cover the topic of mixed types validation.
I have tried marking the paths as modified, but that doesn't make any difference:
doc.markModified('columns.0.0.state');
What is the solution to SectionSchema validating correctly in this case?

Related

Pushing object to triple nested array in Mongoose

I am trying to figure out how to access and push an object into a triple nested array while using mongoose.
My idea is to divide a blog post into an array of chapters, where each has their own array of pages. Each page will have a title, image and body.
Story schema (parent document)
const mongoose = require('mongoose')
const pageSchema = require('./pageModel')
const storySchema = mongoose.Schema({
storyTitle: {
type: String,
required: [true, 'Title required.']
},
storyAuthor: {
type: String,
required: [true, 'Author required.']
},
storyImage: {
type: String,
required: true,
},
chapters: [{
chapterTitle: {
type: String,
//required: [true, 'Title required.']
},
chapterSummary: {
type: String,
// required: [true, 'Summary required.']
},
pages: [pageSchema]
}]
})
module.exports = mongoose.model('Story', storySchema)
the Page Schema
const mongoose = require('mongoose')
const pageSchema = mongoose.Schema({
pageTitle: {
type: String,
required: [true, 'Title required.']
},
pageBody: {
type: String,
},
pageImage: {
type: String,
}
})
module.exports = pageSchema;
Simple ops
const mongoose = require('mongoose')
const Story = require('./storyModel')
const Page = require('./pageModel')
const test = new Story({
storyTitle: 'Test',
storyAuthor: 'Myself',
storyImage: 'test',
});
console.log(test);
test.chapters.push({
chapterTitle: 'chapter 1',
chapterSummary: 'let us see',
})
const first = {
pageTitle: 'A page',
pageImage: 'image',
pageBody: 'A body'
}
console.log(test)
//test.chapters[0].pages[0].push(page)
test.chapters[0].pages.push(first)
console.log(test)
When I log each step, the parent is correctly made, and I am able to append a chapter to the chapters array. But when I the first object, the change isn't visible.
If I log test.chapters, the pages array shows pages: [ [Object] ]
And if I log test.chapters.pages, I just see 'undefined'.
Any suggestions and critique is appreciated.

Mongoose populate nested element inside of multiple arrays

I have the following schema:
const userSchema = new Schema({
...,
categories: [
{
name: {
type: String,
required: true
},
products: [
{
type: mongoose.Types.ObjectId,
required: false,
ref: 'Product'
}
]
}
],
...
}
I want to get all the products a user have.
I have seen more questions about this topic but I don't get it to work.
If you entered your data correctly you can do like this:
let result = await User.findById(id).populate("categories.products").lean()

Adding Array To Mongo via AJAX Call and Mongoose

I'm trying to update a document in a mongo database with information from a form, with all the form going into a field which is an array. At the moment I can't get it to update a document, only create a new one, but more pressingly I can't get the information from the form into the array.
Here is my schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const WorkoutSchema = new Schema({
day: {
type: Date,
default: Date.now
},
exercises: [
{
type: String,
trim: true,
required: "Exercise type is required"
},
{
name: String,
trim: true,
required: "Exercise name is required"
},
{
duration: Number
},
{
weight: Number
},
{
reps: Number
},
{
sets: Number
},
{
duration: Number
},
{
distance: Number
}
]
});
const Workout = mongoose.model("Workout", WorkoutSchema);
module.exports = Workout;
And here is my API route. I've included the results of console.logs below it so you can see the information that is getting passed.
app.put("/api/workouts/:id", (req, res) => {
console.log("api body: " + JSON.stringify(req.body));
console.log("body is " + typeof req.body);
var body = JSON.stringify(req.body);
// body = body.split("{")[1];
// body = body.split("}")[0];
// body = "["+body+"]";
console.log(body);
Workout.create({exercises: `${body}`})
.then(Workout => {
res.json(Workout);
})
.catch(err => {
res.json(err);
});
});
api body: {"type":"resistance","name":"Test Press","weight":100,"sets":5,"reps":6,"duration":10}
body is object
{"type":"resistance","name":"Test Press","weight":100,"sets":5,"reps":6,"duration":10}
In the database I get exercises as an array with one element - the above object - instead of a series of key/value pairs. I've tried a lot of things, but this is as close as I get to what I'm trying to do.
Can anyone see where I've gone wrong?
This turned out to be a basic syntax error which came about because one of my keys was "type". The issue is in the syntax of the exercises array, the model should look like this:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const WorkoutSchema = new Schema({
day: {
type: Date,
default: Date.now
},
exercises: [{
type: {
type: String,
trim: true,
required: "Exercise type is required"
},
name: {
type: String,
trim: true,
required: "Exercise name is required"
},
duration: {
type: Number,
required: "Duration is required"
},
weight: {
type: Number
},
reps: {
type: Number
},
sets: {
type: Number
},
distance: {
type: Number
}
}]
});
const Workout = mongoose.model("Workout", WorkoutSchema);
module.exports = Workout;

How to validate Array of Objects in Mongoose

I need to validate the array of objects in my schema
Schema:
user: [{
name: String,
Age: String,
Contact: Number
}]
How to validate name, age and contact.
I assume your user array is inside another schema.
Let's say we have a Course model with users like this:
const mongoose = require("mongoose");
const courseSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
users: [
{
name: { type: String, required: [true, "Name is required"] },
age: { type: Number, required: [true, "Age is required"] },
contact: { type: Number, required: [true, "Contact is required"] }
}
]
});
const Course = mongoose.model("Post", courseSchema);
module.exports = Course;
To validate this in a post route you can use mongoose model validateSync method:
const Course = require("../models/course");
router.post("/course", async (req, res) => {
const { name, users } = req.body;
const course = new Course({ name, users });
const validationErrors = course.validateSync();
if (validationErrors) {
res.status(400).send(validationErrors.message);
} else {
const result = await course.save();
res.send(result);
}
});
When we send a requset body without required fields like age and contact:
(you can also transform validationErrors.errors for more useful error messages.)
{
"name": "course 1",
"users": [{"name": "name 1"}, {"name": "name 2", "age": 22, "contact": 2222}]
}
The result will be like this:
Post validation failed: users.0.contact: Contact is required, users.0.age: Age is required
It will be similar to the usual validation but inside an array, you need to make a validator function as so:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
//the custom validation that will get applied to the features attribute.
var notEmpty = function(features){
if(features.length === 0){return false}
else {return true};
}
var CarSchema = new Schema({
name: {
type: String,
required: true,
},
features: [{
type: Schema.ObjectId,
ref: Feature
required: true; //this will prevent a Car model from being saved without a features array.
validate: [notEmpty, 'Please add at least one feature in the features array'] //this adds custom validation through the function check of notEmpty.
}]
});
var FeatureSchema = new Schema({
name: {
type: String,
required: true //this will prevent a Feature model from being saved without a name attribute.
}
});
mongoose.model('Car', CarSchema);
mongoose.model('Feature', FeatureSchema);
By using type key/property:
var breakfastSchema = new Schema({
eggs: {
type: Number,
min: [6, 'Too few eggs'],
max: 12
},
bacon: {
type: Number,
required: [true, 'Why no bacon?']
},
drink: {
type: String,
enum: ['Coffee', 'Tea'],
required: function() {
return this.bacon > 3;
}
}
});

Saving Mongoose documents with empty sub-documents collections results in duplicate key error

I have two mongoose schemas:
var productSchema = new Schema({
name: { type: String, required: true, unique: true },
...
});
...
var categorySchema = new Schema({
...
products: [ProductSchema]
});
When I try to save categories
var categories = [
{..., products: []},
{..., products: []}
];
or even without products
var categories = [
{...},
{...}
];
I'm getting error
{ [MongoError: E11000 duplicate key error index: test.categories.$products.name_1 dup key: { : undefined }]
name: 'MongoError',
err: 'E11000 duplicate key error index: test.categories.$products.name_1 dup key: { : undefined }',
code: 11000,
n: 0,
lastOp: { _bsontype: 'Timestamp', low_: 6, high_: 1404282198 },
ok: 1 }
It seems like mongoose is trying to save products with undefind names.
Mongoose log before getting error:
Mongoose: categories.insert({ __v: 0, products: [], _id: ObjectId("53b3c167d28a86102dec420a"), order: 1, description: 'Category 1', name: 'Cat 1' }) {}
Mongoose: categories.insert({ __v: 0, products: [], _id: ObjectId("53b3c167d28a86102dec420b"), order: 2, description: 'Category 2', name: 'Cat 2' }) {}
If I remove unique: true from the name property of productSchema two categories are added with empty products [] collections.
What am I doing wrong?
Thank you!
This is pretty normal really. The empty array is essentially considered to be a "null" value for the "products.name" field, that of course violates the unique constraint on the index.
You could essentially "skip" any values of "name" that are in fact undefined, and you do this by adding the "sparse" property to the index. In the present schema path form:
var productSchema = new Schema({
name: { type: String, required: true, unique: true, sparse: true }
});
var categorySchema = new Schema({
products: [productSchema]
});
Now as long as there is no value in "name" there will be no problem unless of course it actually exists somewhere. Make sure to drop the index already created first though.
Just a note, be aware that what this is doing is making sure that the "products.name" values are unique for the "whole" collection. If you are just trying to make sure they are unique for a given category, then indexing is not your solution and you need to ensure that by other means.

Categories

Resources