Permalink
Browse files

Throw is a value passed to numItems is not an object or an array.

  • Loading branch information...
1 parent 47bd2a5 commit aeb0b31342cfb7b8444e5cebd17c4d419009ebf1 @Kami Kami committed May 7, 2013
Showing with 8 additions and 3 deletions.
  1. +8 −3 lib/validators.js
View
@@ -1,5 +1,7 @@
var check = require('validator').check;
+var utils = require('./util');
+
exports.isPort = function(value, baton) {
value = parseInt(value, 10);
@@ -43,14 +45,17 @@ exports.isHostname = function(value) {
* @param {Number} max Maximum number of elements.
*/
exports.numItems = function(value, min, max) {
- var len;
+ var len, type = utils.typeOf(value);
- if (value instanceof Array) {
+ if (type === 'array') {
len = value.length;
}
- else {
+ else if (type === 'object') {
len = Object.keys(value).length;
}
+ else {
+ throw new Error('value must either be a array or an object');
+ }
if (len < min || len > max) {
throw new Error('Object needs to have between ' + min + ' and ' + max + ' items');

0 comments on commit aeb0b31

Please sign in to comment.