Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make numItems work more like isOptional validator and perform actual …

…checks

inside the isHash / isArray validator.
  • Loading branch information...
commit fdd02da2cfdacbf8d693d0291bd6d876778fd43e 1 parent 4adae3d
@Kami Kami authored
Showing with 78 additions and 15 deletions.
  1. +24 −0 lib/validators.js
  2. +39 −14 lib/valve.js
  3. +15 −1 tests/test-valve.js
View
24 lib/validators.js
@@ -34,3 +34,27 @@ exports.isHostname = function(value) {
return true;
};
+
+/**
+ * Verify that the provided array or object has between min and max number of
+ * elements.
+ * @param {Object} value Object or array to validate.
+ * @param {Number} min Minimum number of elements.
+ * @param {Number} max Maximum number of elements.
+ */
+exports.numItems = function(value, min, max) {
+ var len;
+
+ if (value instanceof Array) {
+ len = value.length;
+ }
+ else {
+ len = Object.keys(value).length;
+ }
+
+ if (len < min || len > max) {
+ throw new Error('Object needs to have between ' + min + ' and ' + max + ' items');
+ }
+
+ return value;
+};
View
53 lib/valve.js
@@ -213,12 +213,14 @@ var Chain = function() {
if (! (this instanceof Chain)) {
return new Chain();
}
+
this.validators = [];
this.target = null;
this.isOptional = false;
this.isImmutable = false;
this.isUpdateRequired = false;
this._validatorCount = 0;
+ this._numItemsValidator = null;
};
@@ -1332,7 +1334,6 @@ Chain.prototype.len = function(min, max) {
return this;
};
-
/**
* Verify that the provided array or object has between min and max number of
* elements.
@@ -1350,21 +1351,16 @@ Chain.prototype.numItems = function(min, max) {
max = Infinity;
}
- v.func = function(value, baton, callback) {
- var len;
-
- if (value instanceof Array) {
- len = value.length;
- }
- else {
- len = Object.keys(value).length;
- }
+ if (this._numItemsValidator) {
+ throw new Error('Chain can only have a single numItems validator');
+ }
- if (len < min || len > max) {
- callback('Object needs to have between ' + min + ' and ' + max + ' items');
- return;
- }
+ this._numItemsValidator = {
+ 'min': min,
+ 'max': max
+ }
+ v.func = function(value, baton, callback) {
callback(null, value);
};
@@ -1828,6 +1824,15 @@ Chain.prototype.isArray = function(chain) {
callback('Not an array');
return;
}
+
+ try {
+ self._validateNumItems(value);
+ }
+ catch (e) {
+ callback(e.message);
+ return;
+ }
+
async.map(value,
function(item, itercb) {
checkChain(item, chain, baton, itercb);
@@ -1883,15 +1888,26 @@ Chain.prototype.isHash = function(keyChain, valueChain) {
func: function(value, baton, callback) {
var key,
kvpairs = [];
+
if (typeOf(value) !== 'object') {
callback('Not a hash');
return;
}
+
+ try {
+ self._validateNumItems(value);
+ }
+ catch (e) {
+ callback(e.message);
+ return;
+ }
+
for (key in value) {
if (value.hasOwnProperty(key)) {
kvpairs.push([key, value[key]]);
}
}
+
async.reduce(kvpairs, {}, iter(baton), callback);
},
help: 'Hash [' + chainHelp(keyChain).join(',') + ':' +
@@ -1900,6 +1916,15 @@ Chain.prototype.isHash = function(keyChain, valueChain) {
return this;
};
+Chain.prototype._validateNumItems = function(value) {
+ if (!this._numItemsValidator) {
+ return;
+ }
+
+ validators.numItems(value, this._numItemsValidator.min,
+ this._numItemsValidator.max);
+};
+
/**
* Adds a validator to the chain that reassigns the value for the
View
16 tests/test-valve.js
@@ -124,7 +124,7 @@ var badExampleNode1 = {
exports['test_validate_numItems'] = function(test, assert) {
- var v1, v2, v3;
+ var v1, v2, v3, thrown = false;
v1 = new V({
a: C().isArray(C().isInt()).numItems(1, 5)
@@ -138,6 +138,20 @@ exports['test_validate_numItems'] = function(test, assert) {
a: C().isArray(C().isInt()).numItems(2)
});
+ try {
+ new V({
+ a: C().isArray(C().isInt()).numItems(2).numItems(2)
+ });
+ }
+ catch (e) {
+ thrown = true;
+ assert.match(e.message, /single numItems validator/i);
+ }
+
+ if (!thrown) {
+ assert.fail('numItems added multiple times, but exception wasnt thrown');
+ }
+
// Negative test cases (array)
v1.check({'a': [1]}, function(err, cleaned) {
assert.ifError(err);
Please sign in to comment.
Something went wrong with that request. Please try again.