mirror of https://github.com/pelias/api.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
45 lines
1.2 KiB
45 lines
1.2 KiB
9 years ago
|
var check = require('check-types');
|
||
9 years ago
|
|
||
|
var MIN_SIZE = 1,
|
||
|
MAX_SIZE = 40,
|
||
|
DEFAULT_SIZE = 10;
|
||
10 years ago
|
|
||
10 years ago
|
// validate inputs, convert types and apply defaults
|
||
9 years ago
|
function setup( size_min, size_max, size_def ){
|
||
|
|
||
|
// allow caller to inject custom min/max/default values
|
||
|
if( !check.number( size_min ) ){ size_min = MIN_SIZE; }
|
||
|
if( !check.number( size_max ) ){ size_max = MAX_SIZE; }
|
||
|
if( !check.number( size_def ) ){ size_def = DEFAULT_SIZE; }
|
||
|
|
||
|
return function sanitize( raw, clean ){
|
||
|
|
||
|
// error & warning messages
|
||
|
var messages = { errors: [], warnings: [] };
|
||
|
|
||
|
// coercions
|
||
|
clean.size = parseInt( raw.size, 10 );
|
||
|
|
||
|
// invalid numeric input
|
||
|
if( isNaN( clean.size ) ){
|
||
|
clean.size = size_def;
|
||
|
}
|
||
|
// ensure size falls within defined range
|
||
|
else if( clean.size > size_max ){
|
||
|
// set the max size
|
||
|
messages.warnings.push('out-of-range integer \'size\', using MAX_SIZE');
|
||
|
clean.size = size_max;
|
||
|
}
|
||
|
else if( clean.size < size_min ){
|
||
|
// set the min size
|
||
|
messages.warnings.push('out-of-range integer \'size\', using MIN_SIZE');
|
||
|
clean.size = size_min;
|
||
|
}
|
||
|
|
||
|
return messages;
|
||
|
};
|
||
10 years ago
|
}
|
||
|
|
||
|
// export function
|
||
9 years ago
|
module.exports = setup;
|