From c999522c0084a989c5a95ae009196c4c0d11adad Mon Sep 17 00:00:00 2001 From: Aaron Heckmann Date: Thu, 9 Feb 2012 09:27:17 -0500 Subject: [PATCH] fix regexp validators in node >= v0.6 --- lib/schematype.js | 2 +- test/model.test.js | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/lib/schematype.js b/lib/schematype.js index 9d603f4c5d5..a7b956bea4c 100644 --- a/lib/schematype.js +++ b/lib/schematype.js @@ -159,7 +159,7 @@ SchemaType.prototype.get = function (fn) { */ SchemaType.prototype.validate = function (obj, error) { - if ('function' == typeof obj) { + if ('function' == typeof obj || obj && 'RegExp' === obj.constructor.name) { this.validators.push([obj, error]); return this; } diff --git a/test/model.test.js b/test/model.test.js index ed74984a5a1..b59e3d95c2a 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -1122,16 +1122,15 @@ module.exports = { // GH-319 'save callback should only execute once regardless of number of failed validations': function () { - mongoose.model('CallbackFiresOnceValidation', new Schema({ + var db = start() + + var D = db.model('CallbackFiresOnceValidation', new Schema({ username: { type: String, validate: /^[a-z]{6}$/i } , email: { type: String, validate: /^[a-z]{6}$/i } , password: { type: String, validate: /^[a-z]{6}$/i } })); - var db = start() - , CallbackFiresOnceValidation = db.model('CallbackFiresOnceValidation'); - - var post = new CallbackFiresOnceValidation({ + var post = new D({ username: "nope" , email: "too" , password: "short" @@ -1140,6 +1139,7 @@ module.exports = { var timesCalled = 0; post.save(function (err) { + db.close(); err.should.be.an.instanceof(MongooseError); err.should.be.an.instanceof(ValidationError); @@ -1161,7 +1161,6 @@ module.exports = { post.errors.email.message.should.eql('Validator failed for path email'); post.errors.username.message.should.eql('Validator failed for path username'); - db.close(); }); },