Skip to content

Commit

Permalink
fix regexp validators in node >= v0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
aheckmann committed Feb 9, 2012
1 parent 2d50d64 commit c999522
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/schematype.js
Expand Up @@ -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;
}
Expand Down
11 changes: 5 additions & 6 deletions test/model.test.js
Expand Up @@ -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"
Expand All @@ -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);

Expand All @@ -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();
});
},

Expand Down

0 comments on commit c999522

Please sign in to comment.