[Mongoose] You Can Skip Validation with model.save({ validateBeforeSave: false })

Tadashi Shigeoka ·  Fri, February 3, 2017

Since Mongoose 4.2.0, the validateBeforeSave option has been added to skip validation when using model.save().

mongoose | マングース

validateBeforeSave Test Code test/document.test.js#L462-L473

For sample code using the validateBeforeSave option, mongoose’s test code seemed intuitively understandable.

it('allows you to skip validation on save (gh-2981)', function(done) {
  var db = start();

  var MyModel = db.model('gh2981',
      {name: {type: String, required: true}});

  var doc = new MyModel();
  doc.save({validateBeforeSave: false}, function(error) {
    assert.ifError(error);
    db.close(done);
  });
});

Since this is only available from Mongoose 4.2.0, I recommend updating quickly if you’re using an older version.

Reference Information

That’s all from the Gemba.