Skip to content

Commit

Permalink
Added tests to demonstrate that messages were not being bound to Vali…
Browse files Browse the repository at this point in the history
…datorErrors. Added a fix.
  • Loading branch information
bnoguchi committed Feb 24, 2011
1 parent 35ddcc5 commit b76017a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/mongoose/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

function MongooseError (msg) {
Error.call(this, msg);
Error.call(this);
Error.captureStackTrace(this, arguments.callee);
this.message = msg;
this.name = 'MongooseError';
};

Expand Down
27 changes: 27 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,33 @@ module.exports = {
});
},

'test validation with custom message': function () {
function validate (val) {
return val === 'abc';
}
mongoose.model('TestValidationMessage', new Schema({
simple: { type: String, validate: [validate, 'must be abc'] }
}));

var db = start()
, TestValidationMessage = db.model('TestValidationMessage');

var post = new TestValidationMessage();
post.set('simple', '');

post.save(function(err){
err.should.be.an.instanceof(MongooseError);
err.should.be.an.instanceof(ValidatorError);
err.message.should.equal('Validator "must be abc" failed for path simple');

post.set('simple', 'abc');
post.save(function(err){
should.strictEqual(err, null);
db.close();
});
});
},

'test required validation for undefined values': function () {
mongoose.model('TestUndefinedValidation', new Schema({
simple: { type: String, required: true }
Expand Down

0 comments on commit b76017a

Please sign in to comment.