Skip to content

Commit

Permalink
use validateBeforeSave in save() to be consistent with schema option (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Oct 8, 2015
1 parent 012405b commit 3c50fec
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
4 changes: 3 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,9 @@ Model.prototype.$__handleSave = function(options, callback) {
* [the `toObject.retainKeyOrder` option](http://mongoosejs.com/docs/api.html#document_Document-toObject)
* to true on your schema.
*
* @param {Object} [options] options set `options.safe` to override [schema's safe option](http://mongoosejs.com//docs/guide.html#safe)
* @param {Object} [options] options optional options
* @param {Object} [options.safe] overrides [schema's safe option](http://mongoosejs.com//docs/guide.html#safe)
* @param {Boolean} [options.validateBeforeSave] set to false to save without validating.
* @param {Function} [fn] optional callback
* @return {Promise} Promise
* @api public
Expand Down
15 changes: 11 additions & 4 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,12 +185,19 @@ Object.defineProperty(Schema.prototype, '_defaultMiddleware', {
return next();
}

var hasNoSkipValidationOption = !options ||
typeof options !== 'object' ||
options.validateOnSave !== false;
var hasValidateBeforeSaveOption = options &&
(typeof options === 'object') &&
('validateBeforeSave' in options);

var shouldValidate;
if (hasValidateBeforeSaveOption) {
shouldValidate = !!options.validateBeforeSave;
} else {
shouldValidate = this.schema.options.validateBeforeSave;
}

// Validate
if (hasNoSkipValidationOption && this.schema.options.validateBeforeSave) {
if (shouldValidate) {
// HACK: use $__original_validate to avoid promises so bluebird doesn't
// complain
if (this.$__original_validate) {
Expand Down
2 changes: 1 addition & 1 deletion test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ describe('document', function() {
{ name: { type: String, required: true } });

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

0 comments on commit 3c50fec

Please sign in to comment.