mongoose-reference-validator
v1.0.0
Published
validator for external reference documents
Downloads
7
Maintainers
Readme
mongoose-reference-validator
validator for external reference documents
usage
schema setup
var mongoose = require('mongoose');
var ObjectId = mongoose.Schema.Types.ObjectId;
var mrf = require('mongoose-reference-validator');
var user = new mongoose.Schema({ name: String });
mongoose.model('user', user);
var car = new mongoose.Schema({
name: String,
driver : { type: ObjectId, ref: 'user' },
});
mrf(car);
mongoose.model('car', car);
document interaction
var jetta = new car({ name : "Jetta", user : user._id });
jetta.save(function(err, doc) {
if (err && err.errors.driver.message) {
console.log('user._id was invalid');
console.log(err.errors.driver.message); // driver does not exist
}
});
The validator will run when being saved, however, you can also run while updating.