express-validator to validate parameter which is an array - node.js

I am using express-validator to validate POST data in my express application. I have a form which has a select where in user can select multiple options:
<select name="category" multiple id="category">
<option value="1">category 1 </option>
.......
</select>
The payload after submitting form shows me this if I select multiple values:
...&category=1&category=2&....
Now, in my Express application I try to validate it like this:
req.checkBody('category', 'category cannot be empty').notEmpty();
But, even after I send multiple values I always get the error - category cannot be empty. If I print my variable as req.body.category[0] - I get the data. But, somehow not able to understand the way I need to pass this to my validator.

You may need to create your own custom validator;
expressValidator = require('express-validator');
validator = require('validator');
app.use(expressValidator({
customValidators: {
isArray: function(value) {
return Array.isArray(value);
},
notEmpty: function(array) {
return array.length > 0;
}
gte: function(param, num) {
return param >= num;
}
}
}));
req.checkBody('category', 'category cannot be empty').isArray().notEmpty();

Try this:
router.post('/your-url',
[
check('category').custom((options, { req, location, path }) => {
if (typeof category === 'object' && category && Array.isArray(category) && category.length) {
return true;
} else {
return false;
}
})
],
controller.fn);

Answering a bit late but hope this might help somebody.
If you are using express-validator, You can pass minimum / maximum length option to check whether Array is empty or exceeds a certain limit
req.checkBody('category', 'category cannot be empty').isArray({ min: 1, max: 10 });

If you are using schema, you can do it this way:
const schema = {
someArray: { isArray: true, notEmpty: true }
};

This maybe a bit late but for others who wants a clean solution without using the customValidator, I created a validator that can dig up to the deepest array and validate its content.
https://github.com/edgracilla/wallter
In your case validation would be:
const halter = require('wallter').halter
const Builder = require('wallter').builder // validation schema builder
const builder = new Builder()
server.use(halter())
server.post('/test', function (req, res, next) {
let validationSchema = builder.fresh()
.addRule('category.*', 'required')
.build()
// validationSchema output:
// { 'category.*': { required: { msg: 'Value for field \'category.*\' is required' } } }
req.halt(validationSchema).then(result => {
if (result.length) {
res.send(400, result)
} else {
res.send(200)
}
return next()
})
})
Error message can be override in builder initialization. Check the repo README.md for an in depth usage.

Related

Check if the body parameter is not null and update on MongoDB

I'm trying to update a document in MongoDB using NodeJS (NextJS). My current code is:
import connect from "../../util/mongodb";
async function api(req, res) {
if (req.method === "POST") {
const { id } = req.body;
const { name } = req.body;
const { email} = req.body;
const { anything1 } = req.body;
const { anything2 } = req.body;
if (!id) {
res.status(400).json({ "error": "missing id param" });
return;
}
const { db } = await connect();
const update = await db.collection("records_collection").findOneAndUpdate(
{ id },
{
$set: {
name,
email,
anything1,
anything2
}
},
{ returnOriginal: false }
);
res.status(200).json(update);
} else {
res.status(400).json({ "error": "wrong request method" });
}
}
export default api;
Everything is working. But I would like to request only the ID as mandatory, and for the other information, leave optional.
In this code, passing the id and name for example, the other three fields (email, anything1 and anything2) will be null in the document.
It is possible to implement the update without requiring all document information and ignore when body fields are null? (As a beginner in NodeJS and MongoDB, the only way to do that that comes to my head now is to surround it all by a lot of if...)
If I've understood your question correctly you can achieve your requirement using the body object in $set stage.
If there is a field which not exists in the object, mongo will not update that field.
As an example check this example where only name field is updated and the rest of fields are not set null.
Another example with 2 fields updated and 3 fields.
You can see how only is updated the field into object in the $set.
So you only need to pass the object received into the query. Something like this:
const update = await db.collection("records_collection").findOneAndUpdate(
{ id },
{
$set: req.body
},
{ returnOriginal: false }
);

Create a new mongoose object with optional parameters from a request

I have a fairly long and complicated nested objected with optional objects inside (see below). I have a request coming in that could have some fields/objects missing - how can I handle that? Can I just use the "?" operand - I have tried but can't seem to get it working. Is there another method I could try?
const product = new Product({
prodID: req.body.prodID,
prodComponents: {
textComponent :{
item: req.body.prodComponents.textComponent.item ? req.body.prodComponents.textComponent.item : null
}
imageComponent:{
item: req.body.prodComponents.imageComponent.item ? req.body.prodComponents.imageComponent.item : null
}
}
)};
There are plenty of ways to resolve your problem. The variable: req.body is just another object as almost everything in javascript.
I'd recommend taking that object and parse it into variables or other object that your program or data model will handle with no exceptions of access into empty or undefined fields.
function createProductDtoToProduct(data) {
const model = {
prodID: data.prodID,
prodComponents: {
textComponent: {
item: null
},
imageComponent: {
item: null
}
}
};
if (data.prodComponents && data.prodComponents.textComponentItem)
model.prodComponents.textComponent.item = {
id: data.textComponentItem.id,
title: prodComponents.textComponentItem.title || "defaultTitle",
}
if (data.prodComponents && data.prodComponents.imageComponentItem)
model.prodComponents.imageComponent.item = {
id: data.imageComponentItem.id,
title: prodComponents.imageComponentItem.title || "defaultTitle",
}
return model;
}
router.post("/createProduct", async (req, res) => {
const product = new Product(createProductDtoToProduct(req.body));
await db.save(product);
return "OK";
});
Also you can validate the body of the request with many tools such as:
https://express-validator.github.io/docs/

Update a field in an array of subdocuments for ALL records

When I first created my site I didn't know MongoDB did case sensitive searching, among many other things, and I'm trying to fix the problems.
I've already updated the code to alter the user input before it's saved so it is lowercase AND trimmed of extra spaces.
I can easily update a collection of users from the mongo shell, and then I'm good to go there, but I have another collection that needs updating and it looks like this:
Registration Data Object:
{
event: string,
field: string,
users: [string],
players: [
first: string,
last: string,
email: string
]
}
If it's possible to update the players.email field for ALL registrations by trimming it and making it lowercase from the mongo shell, I'd love to do that. But I don't think it is, and I've had trouble doing it from mongoose (using Node.js backend, AngularJS frontend).
I was thinking something like this, but I get Type error: Registration.save is not a function:
module.exports.regUpdateAll = function(req, res) {
console.log('UPDATE ALL REGISTRATIONS');
Registration.find().exec((err, reg) => {
reg.forEach((reg) => {
for(var i = 0; i < reg.players.length; i++) {
reg.players[i].email = reg.players[i].email.toLowerCase().trim();
}
});
console.log(reg);
Registration.save(reg).then((err, response) => {
if(!err) {
res.status(200).send(response);
} else {
console.log(err);
res.status(500).send(err);
}
});
});
};
How can I fix this to work?
You need to call .save() from the object of Registration model. for example
Registration.find().exec((err, regtrations) => {
regtrations.forEach((reg) => {
reg.players =
reg.players.map( p=> {
p.email = p.email.toLowerCase().trim();
return p;
})
console.log(reg);
reg.markModified("players");
reg.save( (error)=> {
//do something
})
})
});
I GOT IT! This took ALL DAY, but I finally got it to work. I could NOT get it to work based on the value of has_signed, but I was able to do it this way:
this.adminService.GetUnsignedWaivers()
.subscribe((data: []) => {
this.players = data;
this.players.forEach((p) => {
p.waivers =
p.waivers.map( w=> {
if(w.signatureUrl.length>0) {
w.url = w.signatureUrl;
w.message = "View Waiver";
}
return w;
});
});
this.size = this.players.length;
console.log(this.players);
});
I just don't understand WHY.

Mongodb/mongoose omit a field in response [duplicate]

I have a NodeJS application with Mongoose ODM(Mongoose 3.3.1). I want to retrieve all fields except 1 from my collection.For Example: I have a collection Product Which have 6 fields,I want to select all except a field "Image" . I used "exclude" method, but got error..
This was my code.
var Query = models.Product.find();
Query.exclude('title Image');
if (req.params.id) {
Query.where('_id', req.params.id);
}
Query.exec(function (err, product) {
if (!err) {
return res.send({ 'statusCode': 200, 'statusText': 'OK', 'data': product });
} else {
return res.send(500);
}
});
But this returns error
Express
500 TypeError: Object #<Query> has no method 'exclude'.........
Also I tried, var Query = models.Product.find().exclude('title','Image'); and var Query = models.Product.find({}).exclude('title','Image'); But getting the same error. How to exclude one/(two) particular fields from a collection in Mongoose.
Use query.select for field selection in the current (3.x) Mongoose builds.
Prefix a field name you want to exclude with a -; so in your case:
Query.select('-Image');
Quick aside: in JavaScript, variables starting with a capital letter should be reserved for constructor functions. So consider renaming Query as query in your code.
I don't know where you read about that .exclude function, because I can't find it in any documentation.
But you can exclude fields by using the second parameter of the find method.
Here is an example from the official documentation:
db.inventory.find( { type: 'food' }, { type:0 } )
This operation returns all documents where the value of the type field is food, but does not include the type field in the output.
Model.findOne({ _id: Your Id}, { password: 0, name: 0 }, function(err, user){
// put your code
});
this code worked in my project. Thanks!! have a nice day.
You could do this
const products = await Product.find().select(['-image'])
I am use this with async await
async (req, res) => {
try {
await User.findById(req.user,'name email',(err, user) => {
if(err || !user){
return res.status(404)
} else {
return res.status(200).json({
user,
});
}
});
} catch (error) {
console.log(error);
}
In the updated version of Mongoose you can use it in this way as below to get selected fields.
user.findById({_id: req.body.id}, 'username phno address').then(response => {
res.status(200).json({
result: true,
details: response
});
}).catch(err => {
res.status(500).json({ result: false });
});
I'm working on a feature. I store a userId array name "collectedUser" than who is collected the project. And I just want to return a field "isCollected" instead of "collectedUsers". So select is not what I want. But I got this solution.
This is after I get projects from database, I add "isCollected".
for (const item of projects) {
item.set("isCollected", item.collectedUsers.includes(userId), {
strict: false,
})
}
And this is in Decorator #Schema
#Schema({
timestamps: true,
toObject: {
virtuals: true,
versionKey: false,
transform: (doc, ret, options): Partial<Project> => {
return {
...ret,
projectManagers: undefined,
projectMembers: undefined,
collectedUsers: undefined
}
}
}
})
Finally in my controller
projects = projects.map(i => i.toObject())
It's a strange tricks that set undefined, but it really work.
Btw I'm using nestjs.
You can do it like this
const products = await Product.find().select({
"image": 0
});
For anyone looking for a way to always omit a field - more like a global option rather than doing so in the query e.g. a password field, using a getter that returns undefined also works
{
password: {
type: String,
required: true,
get: () => undefined,
},
}
NB: Getters must be enabled with option { toObject: { getters:true } }
you can exclude the field from the schema definition
by adding the attribute
excludedField : {
...
select: false,
...
}
whenever you want to add it to your result,
add this to your find()
find().select('+excludedFiled')

Mongoose helper method has no findOne method?

How can I add helper methods to find and save objects in Mongoose. A friend told me to use helper methods but I cannot get them to work after a day. I always receive errors saying that either findOne() or save() does not exist OR that next callback is undefined (when node compiles ... before I execute it):
I've tried _schema.methods, _schema.statics... nothing works...
var email = require('email-addresses'),
mongoose = require('mongoose'),
strings = require('../../utilities/common/strings'),
uuid = require('node-uuid'),
validator = require('validator');
var _schema = new mongoose.Schema({
_id: {
type: String,
trim: true,
lowercase: true,
default: uuid.v4
},
n: { // Name
type: String,
required: true,
trim: true,
lowercase: true,
unique: true,
index: true
}
});
//_schema.index({
// d: 1,
// n: 1
//}, { unique: true });
_schema.pre('save', function (next) {
if (!this.n || strings.isNullOrWhitespace(this.n)){
self.invalidate('n', 'Domain name required but not supplied');
return next(new Error('Domain name required but not supplied'));
}
var a = email.parseOneAddress('test#' + this.n);
if (!a || !a.local || !a.domain){
self.invalidate('n', 'Name is not valid domain name');
return next(new Error('Name is not valid domain name'));
}
next();
});
_schema.statics.validateForSave = function (next) {
if (!this.n || strings.isNullOrWhitespace(this.n)){
return next(new Error('Domain name required but not supplied'));
}
var a = email.parseOneAddress('test#' + this.n);
if (!a || !a.local || !a.domain){
return next(new Error('Name is not valid domain name'));
}
next();
}
_schema.statics.findUnique = function (next) {
this.validateForSave(function(err){
if (err){ return next(err); }
mongoose.model('Domain').findOne({ n: this.n }, next);
//this.findOne({ n: this.n }, next);
});
}
_schema.statics.init = function (next) {
this.findUnique(function(err){
if (err){ return next(err); }
this.save(next);
});
}
var _model = mongoose.model('Domain', _schema);
module.exports = _model;
I believe you are running into issues because of your usage with this. Every time you enter a new function this's context is changing. You can read more about this at mdn.
Additionally your callbacks aren't allowing anything to be passed into the mongoose method. For example if I was to create the most basic "save" method I I would do the following:
_schema.statics.basicCreate = function(newDocData, next) {
new _model(newDocData).save(next);
}
Now if I wanted to search the Domain collection for a unique document I would use the following:
_schema.statics.basicSearch = function(uniqueName, next) {
var query = {n: uniqueName};
_model.findOne(query, function(err, myUniqueDoc) {
if (err) return next(err);
if (!myUniqueDoc) return next(new Error("No Domain with " + uniqueName + " found"));
next(null, myNewDoc);
});
}
Mongoose has built in validations for what you are doing:
_schema.path("n").validate(function(name) {
return name.length;
}, "Domain name is required");
_schema.path("n").validate(function(name) {
var a = email.parseOneAddress("test#" + name);
if (!a || !a.local || !a.domain) {
return false;
}
return true;
}, "Name is not a valid domain name");
It returns a boolean. If false, it passes an error to the .save() callback with the stated message. For validating uniqueness:
_schema.path("n").validate(function(name, next) {
var self = this;
this.model("Domain").findOne({n: name}, function(err, domain) {
if (err) return next(err);
if (domain) {
if (self._id === domain._id) {
return next(true);
}
return next(false);
}
return next(true);
});
}, "This domain is already taken");
You're using self = this here so that you can access the document inside the findOne() callback. false is being passed to the callback if the name exists and the result that is found isn't the document itself.
I've tried _schema.methods, _schema.statics
To clarify, .statics operate on the Model, .methods operate on the document. Zane gave a good example of statics, so here is an example of methods:
_schema.methods.isDotCom = function() {
return (/.com/).test(this.n);
}
var org = new Domain({n: "stuff.org"});
var com = new Domain({n: "things.com"});
org.isDotCom(); // false
com.isDotCom(); // true
Opinion: It's neat to have mongoose do validations, but it's very easy to forget it's happening. You also may want to have some validation in one area of your app while using different validations elsewhere. I'd avoid using most of it unless you definitively know you will have to do the same thing every time and will never NOT have to do it.
Methods/statics are a different story. It's very convenient to call isDotCom() instead of writing out a regex test every time you need to check. It performs a single and simple task that saves you some typing and makes your code more readable. Using methods for boolean checks can add a ton of readability. Defining statics like findByName (Zane's basicSearch) is great when you know you're going to do a simple query like that repeatedly.
Treat Mongoose as a utility, not as core functionality.

Resources