Skip to content

Instantly share code, notes, and snippets.

@danared
Created March 27, 2015 18:52
Show Gist options
  • Save danared/f7ed8a8f3651e152478d to your computer and use it in GitHub Desktop.
Save danared/f7ed8a8f3651e152478d to your computer and use it in GitHub Desktop.
/*
* Any schema with this plugin will run validators on
* `findOneAndUpdate()` by default.
*/
var runValidatorsPlugin = function(schema, options) {
schema.pre('findOneAndUpdate', function(next) {
this.options.runValidators = true;
next();
});
};
var breakfastSchema = new mongoose.Schema({
steak: {
type: String,
required: true,
enum: ['flank', 'ribeye'],
},
eggs: {
type: Number,
required: true,
min: 2
}
});
// Attach the plugin to `breakfastSchema`
breakfastSchema.plugin(runValidatorsPlugin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment