Comparing version 1.2.0 to 1.3.0
1.3.0 / 2011-04-19 | ||
=================== | ||
* changed; save() callbacks now fire only once on failed validation | ||
* changed; Errors returned from save() callbacks now instances of ValidationError | ||
* fixed; MongooseArray#indexOf now works properly | ||
1.2.0 / 2011-04-11 | ||
@@ -3,0 +10,0 @@ =================== |
@@ -38,2 +38,3 @@ | ||
this.isNew = true; | ||
this.errors = undefined; | ||
}; | ||
@@ -406,3 +407,3 @@ | ||
, validating = {} | ||
, didErr = false; | ||
, validationError = null; | ||
@@ -418,9 +419,9 @@ if (!this.activePaths.some('require', 'init', 'modify')) return next(); | ||
p.doValidate(self.getValue(path), function(err){ | ||
p.doValidate(self.getValue(path), function (err) { | ||
if (err) { | ||
didErr = true; | ||
return next(err); | ||
validationError = validationError || new ValidationError(self); | ||
validationError.errors[err.path] = err.message; | ||
} | ||
--total || next(); | ||
}, self); | ||
--total || next(validationError); | ||
}); | ||
@@ -432,3 +433,3 @@ }); | ||
this.activePaths.forEach('require', 'init', 'modify', function (path) { | ||
if (!didErr) validatePath(path); | ||
validatePath(path); | ||
}); | ||
@@ -685,2 +686,27 @@ | ||
/** | ||
* Document Validation Error | ||
*/ | ||
function ValidationError (instance) { | ||
MongooseError.call(this, "Validation failed"); | ||
Error.captureStackTrace(this, arguments.callee); | ||
this.name = 'ValidationError'; | ||
this.errors = instance.errors = {}; | ||
}; | ||
ValidationError.prototype.toString = function () { | ||
return this.name + ': ' + Object.keys(this.errors).map(function (key) { | ||
return this.errors[key]; | ||
}, this).join(', '); | ||
}; | ||
/** | ||
* Inherits from MongooseError. | ||
*/ | ||
ValidationError.prototype.__proto__ = MongooseError.prototype; | ||
Document.ValidationError = ValidationError; | ||
/** | ||
* Document Error | ||
@@ -693,3 +719,3 @@ * | ||
MongooseError.call(this, msg); | ||
MongooseError.captureStackTrace(this, arguments.callee); | ||
Error.captureStackTrace(this, arguments.callee); | ||
this.name = 'DocumentError'; | ||
@@ -696,0 +722,0 @@ }; |
@@ -284,3 +284,3 @@ | ||
exports.version = '1.2.0'; | ||
exports.version = '1.3.0'; | ||
@@ -287,0 +287,0 @@ /** |
@@ -248,3 +248,3 @@ | ||
/** | ||
* Validator error | ||
* Schema validator error | ||
* | ||
@@ -257,3 +257,6 @@ * @param {String} path | ||
function ValidatorError (path, msg) { | ||
MongooseError.call(this, 'Validator "' + msg + '" failed for path ' + path); | ||
msg = msg | ||
? '"' + msg + '" ' | ||
: ''; | ||
MongooseError.call(this, 'Validator ' + msg + 'failed for path ' + path); | ||
Error.captureStackTrace(this, arguments.callee); | ||
@@ -260,0 +263,0 @@ this.name = 'ValidatorError'; |
@@ -277,2 +277,18 @@ | ||
/** | ||
* Return the index of `obj` or `-1.` | ||
* | ||
* @param {Object} obj | ||
* @return {Number} | ||
* @api public | ||
*/ | ||
MongooseArray.prototype.indexOf = function(obj){ | ||
for (var i = 0, len = this.length; i < len; ++i) { | ||
if (obj == this[i]) | ||
return i; | ||
} | ||
return -1; | ||
}; | ||
/** | ||
* Module exports. | ||
@@ -279,0 +295,0 @@ */ |
{ | ||
"name": "mongoose" | ||
, "description": "Mongoose MongoDB ORM" | ||
, "version": "1.2.0" | ||
, "version": "1.3.0" | ||
, "author": "Guillermo Rauch <guillermo@learnboost.com>" | ||
@@ -12,2 +12,6 @@ , "keywords": ["mongodb", "mongoose", "orm", "data", "datastore", "nosql"] | ||
, "engines": { "node": ">= 0.2.0" } | ||
, "repository": { | ||
"type": "git" | ||
, "url": "git://github.com/LearnBoost/mongoose.git" | ||
} | ||
} |
@@ -129,4 +129,4 @@ Mongoose 1.0 | ||
var instance = new myModel(); | ||
myModel.my.key = 'hello'; | ||
myModel.save(function (err) { | ||
instance.my.key = 'hello'; | ||
instance.save(function (err) { | ||
// | ||
@@ -244,2 +244,13 @@ }); | ||
## Mongoose Plugins | ||
The following plugins are currently available for use with mongoose: | ||
- [mongoose-types](https://github.com/bnoguchi/mongoose-types) - Adds | ||
several additional types (e.g., Email) that you can use in your | ||
Schema declarations | ||
- [mongoose-auth](https://github.com/bnoguchi/mongoose-auth) - A drop in | ||
solution for your auth needs. Currently supports Password, Facebook, | ||
Twitter, Github, and more. | ||
## Contributing to Mongoose | ||
@@ -246,0 +257,0 @@ |
@@ -6,5 +6,22 @@ | ||
var mongoose = require('./common').mongoose | ||
var start = require('./common') | ||
, should = require('should') | ||
, mongoose = require('./common').mongoose | ||
, Schema = mongoose.Schema | ||
, random = require('mongoose/utils').random | ||
, MongooseArray = mongoose.Types.Array; | ||
var User = new Schema({ | ||
name: String | ||
, pets: [Schema.ObjectId] | ||
}); | ||
mongoose.model('User', User); | ||
var Pet = new Schema({ | ||
name: String | ||
}); | ||
mongoose.model('Pet', Pet); | ||
/** | ||
@@ -17,3 +34,3 @@ * Test. | ||
'test that a mongoose array behaves and quacks like an array': function(){ | ||
var a = new MongooseArray(); | ||
var a = new MongooseArray; | ||
@@ -24,4 +41,43 @@ a.should.be.an.instanceof(Array); | ||
(a._atomics.constructor).should.eql(Object); | ||
}, | ||
'test indexOf()': function(){ | ||
var db = start() | ||
, a = new MongooseArray | ||
, User = db.model('User', 'users_' + random()) | ||
, Pet = db.model('Pet', 'pets' + random()); | ||
var tj = new User({ name: 'tj' }) | ||
, tobi = new Pet({ name: 'tobi' }) | ||
, loki = new Pet({ name: 'loki' }) | ||
, jane = new Pet({ name: 'jane' }) | ||
, pets = []; | ||
tj.pets.push(tobi); | ||
tj.pets.push(loki); | ||
tj.pets.push(jane); | ||
var pending = 3; | ||
[tobi, loki, jane].forEach(function(pet){ | ||
pet.save(function(){ | ||
--pending || done(); | ||
}); | ||
}); | ||
function done() { | ||
Pet.find({}, function(err, pets){ | ||
tj.save(function(err){ | ||
User.findOne({ name: 'tj' }, function(err, user){ | ||
should.equal(null, err, 'error in callback'); | ||
user.pets.should.have.length(3); | ||
user.pets.indexOf(tobi.id).should.equal(0); | ||
user.pets.indexOf(loki.id).should.equal(1); | ||
user.pets.indexOf(jane.id).should.equal(2); | ||
db.close(); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
}; |
@@ -13,2 +13,3 @@ | ||
, ValidatorError = SchemaType.ValidatorError | ||
, ValidationError = mongoose.Document.ValidationError | ||
@@ -35,2 +36,3 @@ /** | ||
test: { type: String, required: true } | ||
, work: { type: String, validate: /^good/ } | ||
}); | ||
@@ -43,9 +45,11 @@ | ||
module.exports = { | ||
'test that save fires errors': function(){ | ||
var a = new Subdocument(); | ||
a.set('test', ''); | ||
a.set('work', 'nope'); | ||
a.save(function(err){ | ||
err.should.be.an.instanceof(ValidatorError); | ||
err.should.be.an.instanceof(ValidationError); | ||
err.toString().should.eql('ValidationError: Validator "required" failed for path test, Validator failed for path work'); | ||
}); | ||
@@ -57,2 +61,3 @@ }, | ||
a.set('test', 'cool'); | ||
a.set('work', 'goods'); | ||
@@ -59,0 +64,0 @@ a.save(function(err){ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
0
26460
303
79
3071620
210