Skip to content

Commit

Permalink
Added more key validations, it should now check against /s/n/r in the…
Browse files Browse the repository at this point in the history
… keys
  • Loading branch information
3rd-Eden committed Apr 12, 2012
1 parent a303003 commit a93c356
Showing 1 changed file with 43 additions and 21 deletions.
64 changes: 43 additions & 21 deletions lib/utils.js
Original file line number Original file line Diff line number Diff line change
@@ -1,55 +1,77 @@
"use strict"; "use strict";


var createHash = require('crypto').createHash; var createHash = require('crypto').createHash
, toString = Object.prototype.toString;


exports.validateArg = function validateArg(args, config){ exports.validateArg = function validateArg (args, config) {
var toString = Object.prototype.toString var err
, err
, callback; , callback;


args.validate.forEach(function(tokens){ args.validate.forEach(function (tokens) {
var key = tokens[0] var key = tokens[0]
, value = args[key]; , value = args[key];


switch(tokens[1]){ switch(tokens[1]){
case Number: case Number:
if (toString.call(value) !== '[object Number]') err = 'Argument "' + key + '" is not a valid Number.'; if (toString.call(value) !== '[object Number]') {
err = 'Argument "' + key + '" is not a valid Number.';
}

break; break;


case Boolean: case Boolean:
if (toString.call(value) !== '[object Boolean]') err = 'Argument "' + key + '" is not a valid Boolean.'; if (toString.call(value) !== '[object Boolean]') {
err = 'Argument "' + key + '" is not a valid Boolean.';
}

break; break;


case Array: case Array:
if (toString.call(value) !== '[object Array]') err = 'Argument "' + key + '" is not a valid Array.'; if (toString.call(value) !== '[object Array]') {
err = 'Argument "' + key + '" is not a valid Array.';
}

break; break;


case Object: case Object:
if (toString.call(value) !== '[object Object]') err = 'Argument "' + key + '" is not a valid Object.'; if (toString.call(value) !== '[object Object]') {
err = 'Argument "' + key + '" is not a valid Object.';
}

break; break;


case Function: case Function:
if (toString.call(value) !== '[object Function]') err = 'Argument "' + key + '" is not a valid Function.'; if (toString.call(value) !== '[object Function]') {
err = 'Argument "' + key + '" is not a valid Function.';
}

break; break;


case String: case String:
if (toString.call(value) !== '[object String]') err = 'Argument "' + key + '" is not a valid String.'; if (toString.call(value) !== '[object String]') {

err = 'Argument "' + key + '" is not a valid String.';
if (!err && key === 'key' && value.length > config.maxKeySize){ }
if (config.keyCompression){
args[key] = createHash('md5').update(value).digest('hex');


// also make sure you update the command if (!err && key === 'key') {
args.command.replace(value, args[key]); if (value.length > config.maxKeySize) {
} else { if (config.keyCompression){
err = 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize; args[key] = createHash('md5').update(value).digest('hex');

// also make sure you update the command
args.command.replace(value, args[key]);
} else {
err = 'Argument "' + key + '" is longer than the maximum allowed length of ' + config.maxKeySize;
}
} else if (/[\s\n\r]/.test(value)) {
err = 'The key should not contain any whitespace or new lines';
} }
} }

break; break;


default: default:
if (toString.call(value) === '[object global]' && !tokens[2]) err = 'Argument "' + key + '" is not defined.'; if (toString.call(value) === '[object global]' && !tokens[2]) {
err = 'Argument "' + key + '" is not defined.';
}
} }
}); });


Expand Down

0 comments on commit a93c356

Please sign in to comment.