Skip to content
Browse files

multiple validator errors

  • Loading branch information...
1 parent 6eecdad commit 868eac204c59569ad0176e7a32a5e1ed3dd41606 RGBboy committed Nov 15, 2012
Showing with 31 additions and 7 deletions.
  1. +15 −7 lib/schematype.js
  2. +16 −0 test/schema.test.js
View
22 lib/schematype.js
@@ -541,19 +541,27 @@ SchemaType.prototype.select = function select (val) {
*/
SchemaType.prototype.doValidate = function (value, fn, scope) {
- var err = false
+ var err = null
, path = this.path
, count = this.validators.length;
- if (!count) return fn(null);
+ if (!count) return fn(err);
function validate (val, msg) {
- if (err) return;
- if (val === undefined || val) {
- --count || fn(null);
- } else {
- fn(err = new ValidatorError(path, msg));
+ --count;
+ if (val !== undefined && !val) {
+ if (err) {
+ if (err instanceof ValidatorError) {
+ var errArr = [];
+ errArr.push(err);
+ err = errArr;
+ }
+ err.push(new ValidatorError(path, msg));
+ } else {
+ err = new ValidatorError(path, msg);
+ }
}
+ if (!count) return fn(err);
}
this.validators.forEach(function (v) {
View
16 test/schema.test.js
@@ -466,6 +466,22 @@ describe('schema', function(){
done();
});
+ it('multiple errors', function(done){
+ var MultipleTest = new Schema({
+ testProperty: { type: Number, required: true, min: 1, max: 5 }
+ });
+
+ 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);
+ };
+ done();
+ });
+ });
+
describe('async', function(){
it('works', function(done){
var executed = 0;

0 comments on commit 868eac2

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