Skip to content

Commit

Permalink
Merge pull request Automattic#2043 from sensibill/3.8.x
Browse files Browse the repository at this point in the history
Added in the ability to disable automatic validation when a model is sav...
  • Loading branch information
vkarpov15 committed May 16, 2014
2 parents 45c3ac3 + 79ceb66 commit 715ed59
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
18 changes: 18 additions & 0 deletions docs/guide.jade
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ block content
- [strict](#strict)
- [toJSON](#toJSON)
- [toObject](#toObject)
- [validateBeforeSave](#validateBeforeSave)
- [versionKey](#versionKey)

h4#autoIndex option: autoIndex
Expand Down Expand Up @@ -478,6 +479,23 @@ block content

:markdown
To see all available `toObject` options, read [this](/docs/api.html#document_Document-toObject).

h4#validateOnSave option: validateOnSave
:markdown
By default, documents are automatically validated before they are saved to the database. This is to prevent saving an invalid document. If you want to handle validation manually, and be able to save objects which don't pass validation, you can set validateOnSave to false.

:js
var schema = new Schema({ name: String });
schema.set('validateOnSave', false);
schema.path('name').validate(function (value) {
return v != null;
});
var M = mongoose.model('Person', schema);
var m = new M({ name: null });
m.validate(function(err) {
console.log(err); // Will tell you that null is not allowed.
});
m.save(); // Succeeds despite being invalid

h4#versionKey option: versionKey
:markdown
Expand Down
9 changes: 7 additions & 2 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,10 +1306,15 @@ Document.prototype.$__registerHooks = function () {
} else {
next();
}
}).pre('save', function validation (next) {
return this.validate(next);
});

if(this.schema.options.validateBeforeSave)
{
this.pre('save', function validation (next) {
return this.validate(next);
});
}

// add user defined queues
this.$__doQueue();
};
Expand Down
2 changes: 2 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var EventEmitter = require('events').EventEmitter
* - [strict](/docs/guide.html#strict): bool - defaults to true
* - [toJSON](/docs/guide.html#toJSON) - object - no default
* - [toObject](/docs/guide.html#toObject) - object - no default
* - [validateBeforeSave](/docs/guide.html#validateBeforeSave) - bool - defaults to `true`
* - [versionKey](/docs/guide.html#versionKey): bool - defaults to "__v"
*
* ####Note:
Expand Down Expand Up @@ -166,6 +167,7 @@ Schema.prototype.defaultOptions = function (options) {
, autoIndex: true
, shardKey: null
, read: null
, validateBeforeSave: true
// the following are only applied at construction time
, noId: false // deprecated, use { _id: false }
, _id: true
Expand Down

0 comments on commit 715ed59

Please sign in to comment.