Skip to content

Instantly share code, notes, and snippets.

@muratatak77
Created May 28, 2016 18:36
Show Gist options
  • Save muratatak77/bbe743e379add276cf59cc1fc90b7efb to your computer and use it in GitHub Desktop.
Save muratatak77/bbe743e379add276cf59cc1fc90b7efb to your computer and use it in GitHub Desktop.
Mongoose Validate Array Field and expect self id
CompanySchema
.path('tv_broadcasters')
.validate(function(value, respond) {
var self = this;
this.constructor.find({
tv_broadcasters: {
$in: value
},
_id: {
$ne: self.id
}
}, function(err, company_tvs) {
if (err) throw err;
company_tvs.forEach(function(tv, key) {
console.log("tv_validate : ", tv);
console.log("self.id :", self.id);
console.log("tv.id :", tv._id);
if (self.id === tv._id) return respond(true);
return respond(false);
})
respond(true);
});
}, 'The specified tv_broadcasters is already in use.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment