mirror of https://github.com/pelias/api.git
Julian Simioni
9 years ago
3 changed files with 133 additions and 0 deletions
@ -0,0 +1,57 @@
|
||||
var _ = require('lodash'); |
||||
|
||||
/* |
||||
* Specify an object and array of keys to check. |
||||
* An error will be thrown if at least one, but not all of them are specified |
||||
* |
||||
* @param {object} - the object |
||||
* @param {array} - keys to check |
||||
* |
||||
* returns true if all are present, false if none are present, throws an exception otherwise |
||||
*/ |
||||
function optional_group(object, keys) { |
||||
var contained_in_object = _.contains.bind(null, Object.keys(object)); |
||||
|
||||
if (keys.every(contained_in_object)) { |
||||
return true; |
||||
} else if (!keys.some(contained_in_object)) { |
||||
return false; |
||||
} else { |
||||
throw new Error(error_message(keys)); |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* Specify an object and array of keys to check. |
||||
* An error will be thrown if any of the keys are missing from the object |
||||
*/ |
||||
function required_group(object, keys) { |
||||
var contained_in_object = _.contains.bind(null, Object.keys(object)); |
||||
|
||||
if (keys.every(contained_in_object)) { |
||||
return true; |
||||
} else { |
||||
throw new Error(error_message(keys)); |
||||
} |
||||
} |
||||
|
||||
/* |
||||
* Create a friendly error message from a list of keys |
||||
*/ |
||||
function error_message(keys) { |
||||
var start = 'parameters '; |
||||
|
||||
var listStart = keys.slice(0, -1).join(', '); |
||||
var listEnd = ' and ' + keys[keys.length - 1]; |
||||
|
||||
var adjective = (keys.length > 2) ? 'all' : 'both'; |
||||
|
||||
var end = ' must ' + adjective + ' be specified'; |
||||
|
||||
return start + listStart + listEnd + end; |
||||
} |
||||
|
||||
module.exports = { |
||||
optional: optional_group, |
||||
required: required_group |
||||
}; |
@ -0,0 +1,75 @@
|
||||
var groups = require('../../../sanitiser/_groups'); |
||||
|
||||
module.exports.tests = {}; |
||||
|
||||
module.exports.tests.optional_group = function(test, common) { |
||||
test('optional group none present ', function(t) { |
||||
var object = {}; |
||||
t.doesNotThrow(function() { |
||||
var present = groups.optional(object, [ 'a', 'b' ]); |
||||
t.equal(present, false, 'group reported not present'); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('optional group all present ', function(t) { |
||||
var object = { 'a': 5, 'b': 9 }; |
||||
t.doesNotThrow(function() { |
||||
var present = groups.optional(object, [ 'a', 'b' ]); |
||||
t.equal(present, true, 'group reported present'); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('optional group some present ', function(t) { |
||||
var object = { 'b': 9 }; |
||||
t.throws(function() { |
||||
groups.optional(object, [ 'a', 'b' ]); |
||||
}, new RegExp('parameters a and b must both be specified')); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('optional group some present (larger group) ', function(t) { |
||||
var object = { 'b': 9, 'd': 5 }; |
||||
t.throws(function() { |
||||
groups.optional(object, [ 'a', 'b', 'c', 'd', 'e' ]); |
||||
}, new RegExp('parameters a, b, c, d and e must all be specified')); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.tests.required_group = function(test, common) { |
||||
test('required group none present ', function(t) { |
||||
var object = {}; |
||||
t.throws(function() { |
||||
groups.required(object, [ 'a', 'b' ]); |
||||
}, new RegExp('parameters a and b must both be specified')); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('required group all present ', function(t) { |
||||
var object = { 'a': 5, 'b': 9 }; |
||||
t.doesNotThrow(function() { |
||||
groups.required(object, [ 'a', 'b' ]); |
||||
}); |
||||
t.end(); |
||||
}); |
||||
|
||||
test('required group some present ', function(t) { |
||||
var object = { 'b': 9 }; |
||||
t.throws(function() { |
||||
groups.required(object, [ 'a', 'b' ]); |
||||
}, new RegExp('parameters a and b must both be specified')); |
||||
t.end(); |
||||
}); |
||||
}; |
||||
|
||||
module.exports.all = function (tape, common) { |
||||
function test(name, testFunction) { |
||||
return tape('SANTIZE _groups ' + name, testFunction); |
||||
} |
||||
|
||||
for( var testCase in module.exports.tests ){ |
||||
module.exports.tests[testCase](test, common); |
||||
} |
||||
}; |
Loading…
Reference in new issue