Skip to content
Browse files

added ValidatorCollectionError; updated multiple validator error fix

  • Loading branch information...
1 parent 868eac2 commit b37435eb1651643bc436449a010894d1558d4e65 RGBboy committed Nov 19, 2012
Showing with 57 additions and 9 deletions.
  1. +44 −0 lib/errors/validatorcollection.js
  2. +7 −4 lib/schematype.js
  3. +6 −5 test/schema.test.js
View
44 lib/errors/validatorcollection.js
@@ -0,0 +1,44 @@
+/*!
+ * Module dependencies.
+ */
+
+var MongooseError = require('../error');
+
+/**
+ * Schema validator collection error
+ *
+ * @param {String} path
+ * @param {Array} errors
+ * @inherits MongooseError
+ * @api private
+ */
+
+function ValidatorCollectionError (path, errors) {
+ MongooseError.call(this, 'Validators failed for path ' + path);
+ Error.captureStackTrace(this, arguments.callee);
+ this.name = 'ValidatorCollectionError';
+ this.path = path;
+ this.errors = errors || [];
+};
+
+/*!
+ * toString helper
+ */
+
+ValidatorCollectionError.prototype.toString = function () {
+ return this.name + ': ' + this.errors.map(function (value) {
+ return String(value);
+ }, this).join(', ');
+}
+
+/*!
+ * Inherits from MongooseError
+ */
+
+ValidatorCollectionError.prototype.__proto__ = MongooseError.prototype;
+
+/*!
+ * exports
+ */
+
+module.exports = ValidatorCollectionError;
View
11 lib/schematype.js
@@ -5,6 +5,7 @@
var utils = require('./utils');
var CastError = require('./errors/cast')
var ValidatorError = require('./errors/validator')
+var ValidatorCollectionError = require('./errors/validatorcollection')
/**
* SchemaType constructor
@@ -552,11 +553,11 @@ SchemaType.prototype.doValidate = function (value, fn, scope) {
if (val !== undefined && !val) {
if (err) {
if (err instanceof ValidatorError) {
- var errArr = [];
- errArr.push(err);
- err = errArr;
+ var errCollection = new ValidatorCollectionError(path);
+ errCollection.errors.push(err);
+ err = errCollection;
}
- err.push(new ValidatorError(path, msg));
+ err.errors.push(new ValidatorError(path, msg));
} else {
err = new ValidatorError(path, msg);
}
@@ -610,3 +611,5 @@ module.exports = exports = SchemaType;
exports.CastError = CastError;
exports.ValidatorError = ValidatorError;
+
+exports.ValidatorCollectionError = ValidatorCollectionError;
View
11 test/schema.test.js
@@ -12,6 +12,7 @@ var start = require('./common')
, VirtualType = mongoose.VirtualType
, ObjectId = Schema.ObjectId
, ValidatorError = SchemaType.ValidatorError
+ , ValidatorCollectionError = SchemaType.ValidatorCollectionError
, CastError = SchemaType.CastError
, SchemaTypes = Schema.Types
, DocumentObjectId = mongoose.Types.ObjectId
@@ -472,11 +473,11 @@ describe('schema', function(){
});
MultipleTest.path('testProperty').doValidate(undefined, function (err) {
- assert.ok(err instanceof Array);
- assert.equal(true, Array.isArray(err));
- assert.equal(err.length, 3);
- for (var i = 0; i < err.length; i += 1) {
- assert.ok(err[i] instanceof ValidatorError);
+ assert.ok(err instanceof ValidatorCollectionError);
+ assert.equal(true, Array.isArray(err.errors));
+ assert.equal(err.errors.length, 3);
+ for (var i = 0; i < err.errors.length; i += 1) {
+ assert.ok(err.errors[i] instanceof ValidatorError);
};
done();
});

0 comments on commit b37435e

Please sign in to comment.
Something went wrong with that request. Please try again.