Permalink
Browse files

Fixed some nested errors bugs

  • Loading branch information...
1 parent 60bc533 commit 07082c3eebefc67cfb8442d32535faee8fe01ee1 @masylum masylum committed Oct 17, 2010
Showing with 37 additions and 8 deletions.
  1. +14 −8 lib/validator.js
  2. +23 −0 test/validator.js
View
@@ -7,8 +7,8 @@ module.exports = function (model, data) {
return Object.keys(object).intersect(Object.keys(validator.data));
};
- validator.model = model;
- validator.data = data;
+ validator.model = model || {};
+ validator.data = data || {};
validator.updated_model = utils.merge(utils.clone(validator.model), validator.data);
validator.errors = {};
@@ -33,20 +33,26 @@ module.exports = function (model, data) {
return this.model[attr] !== this.data[attr];
};
- validator.getErrorBucket = function (name) {
+ validator.getErrorBucket = function (name, create) {
var error_bucket = {},
key = null,
name_parts = name.split('.');
if (name_parts.length === 1) {
- this.errors[name] = this.errors[name] || [];
- error_bucket = this.errors[name];
+ if (create) {
+ this.errors[name] = this.errors[name] || [];
+ }
+ return this.errors[name];
} else {
error_bucket = this.errors;
for (;name_parts.length > 0;) {
key = name_parts.shift();
if (!error_bucket[key]) {
- error_bucket[key] = name_parts.length === 0 ? [] : {};
+ if (create) {
+ error_bucket[key] = name_parts.length === 0 ? [] : {};
+ } else {
+ return null;
+ }
}
error_bucket = error_bucket[key];
}
@@ -57,7 +63,7 @@ module.exports = function (model, data) {
validator.hasError = function (name) {
var error_bucket = this.getErrorBucket(name);
- return !utils.isEmpty(this.hasErrors() && error_bucket);
+ return this.hasErrors() && this.getErrorBucket(name, false) !== null;
};
validator.hasErrors = function () {
@@ -104,7 +110,7 @@ module.exports = function (model, data) {
};
validator.addError = function (name, value) {
- var error_bucket = this.getErrorBucket(name);
+ var error_bucket = this.getErrorBucket(name, true);
error_bucket.push(value);
};
View
@@ -2,6 +2,22 @@
* Module dependencies.
*/
+GLOBAL.inspect = require('eyes').inspector({
+ styles: {
+ all: 'yellow',
+ label: 'underline',
+ other: 'inverted',
+ key: 'bold',
+
+ special: 'grey',
+ string: 'green',
+ number: 'red',
+ bool: 'blue',
+ regexp: 'green'
+ },
+ maxLength: 9999999999
+});
+
var validator = require('./../lib/validator');
module.exports['test initial status'] = function (assert) {
@@ -63,6 +79,13 @@ module.exports['test hasError'] = function (assert) {
assert.equal(val.hasError('foo.zemba'), true);
};
+module.exports['test hasError and hasErrors'] = function (assert) {
+ var val = validator({}, {foo: 'bar'});
+
+ assert.equal(val.hasError('foo.zemba'), false);
+ assert.equal(val.hasErrors(), false);
+};
+
// multiple errors per field
module.exports['test multiple errors per field'] = function (assert) {
var val = validator({}, {foo: 'bar'});

0 comments on commit 07082c3

Please sign in to comment.