Browse Source

Merge remote-tracking branch 'origin' into private-param-235

pull/241/head
Diana Shkolnikov 9 years ago
parent
commit
a3b0a090e2
  1. 8
      helper/adminFields.js
  2. 19
      helper/geojsonify.js
  3. 32
      sanitiser/_details.js
  4. 12
      sanitiser/_id.js
  5. 5
      sanitiser/_source.js
  6. 6
      test/unit/helper/geojsonify.js
  7. 2
      test/unit/sanitiser/reverse.js

8
helper/adminFields.js

@ -1,5 +1,7 @@
var peliasSchema = require('pelias-schema');
var peliasLogger = require( 'pelias-logger' ).get( 'api' );
var _ = require('lodash'),
peliasSchema = require('pelias-schema'),
peliasLogger = require( 'pelias-logger' ).get( 'api' );
var ADMIN_FIELDS = [
'admin0',
@ -29,7 +31,7 @@ function getAvailableAdminFields(schema, expectedFields, logger) {
// check if expected fields are actually in current schema
var available = expectedFields.filter(function (field) {
return (actualFields.indexOf(field) !== -1);
return _.contains( actualFields, field );
});
if (available.length === 0) {

19
helper/geojsonify.js

@ -1,9 +1,8 @@
var GeoJSON = require('geojson');
var extent = require('geojson-extent');
var outputGenerator = require('./outputGenerator');
var logger = require('pelias-logger').get('api');
var GeoJSON = require('geojson'),
extent = require('geojson-extent'),
outputGenerator = require('./outputGenerator'),
logger = require('pelias-logger').get('api');
// Properties to be copied
var DETAILS_PROPS = [
@ -104,15 +103,13 @@ function geojsonifyPlace(place) {
return warning('No doc or center_point property');
}
var geocoding = {};
var output = {};
addMetaData(place, geocoding);
addDetails(place, geocoding);
addLabel(place, geocoding);
addMetaData(place, output);
addDetails(place, output);
addLabel(place, output);
var output = {};
output.geocoding = geocoding;
// map center_point for GeoJSON to work properly
// these should not show up in the final feature properties
output.lat = parseFloat(place.center_point.lat);

32
sanitiser/_details.js

@ -0,0 +1,32 @@
var _ = require('lodash'),
check = require('check-types');
var DEFAULT_DETAILS_BOOL = true;
// validate inputs, convert types and apply defaults
function sanitize( raw, clean ){
// error & warning messages
var messages = { errors: [], warnings: [] };
if( !check.undefined( raw.details ) ){
clean.details = isTruthy( raw.details );
} else {
clean.details = DEFAULT_DETAILS_BOOL;
}
return messages;
}
// be lenient with 'truthy' values
function isTruthy(val) {
if( check.string( val ) ){
return _.contains( ['true', '1'], val );
}
return val === 1 || val === true;
}
// export function
module.exports = sanitize;

12
sanitiser/_id.js

@ -1,5 +1,6 @@
var check = require('check-types'),
var _ = require('lodash'),
check = require('check-types'),
types = require('../query/types');
var ID_DELIM = ':';
@ -18,12 +19,7 @@ function sanitize( raw, clean ){
var messages = { errors: [], warnings: [] };
// 'raw.id' can be an array!?
var rawIds = check.array( raw.id ) ? raw.id : [ raw.id ];
// de-dupe ids
rawIds = rawIds.filter(function(item, pos) {
return rawIds.indexOf( item ) === pos;
});
var rawIds = check.array( raw.id ) ? _.unique( raw.id ) : [ raw.id ];
// ensure all elements are valid non-empty strings
rawIds = rawIds.filter( function( uc ){
@ -60,7 +56,7 @@ function sanitize( raw, clean ){
messages.errors.push( errorMessage( rawId ) );
}
// type text must be one of the types
if( types.indexOf( type ) === -1 ){
if( !_.contains( types, type ) ){
messages.errors.push(
errorMessage('type', type + ' is invalid. It must be one of these values - [' + types.join(', ') + ']')
);

5
sanitiser/_source.js

@ -1,5 +1,6 @@
var check = require('check-types'),
var _ = require('lodash'),
check = require('check-types'),
sources_map = require( '../query/sources' );
var ALL_SOURCES = Object.keys(sources_map),
@ -20,7 +21,7 @@ function sanitize( raw, clean ) {
var sources = raw.source.split(',');
var invalid_sources = sources.filter(function(source) {
return ALL_SOURCES.indexOf(source) === -1;
return !_.contains( ALL_SOURCES, source );
});
if( invalid_sources.length > 0 ){

6
test/unit/helper/geojsonify.js

@ -140,7 +140,6 @@ module.exports.tests.search = function(test, common) {
]
},
'properties': {
'geocoding': {
'id': 'id1',
'layer': 'type1',
'source': 'type1',
@ -159,7 +158,6 @@ module.exports.tests.search = function(test, common) {
'street': 'Liverpool Road',
'postalcode': 'N1 0RW'
}
}
},
{
'type': 'Feature',
@ -171,7 +169,6 @@ module.exports.tests.search = function(test, common) {
]
},
'properties': {
'geocoding': {
'id': 'id2',
'layer': 'type2',
'source': 'type2',
@ -186,7 +183,6 @@ module.exports.tests.search = function(test, common) {
'locality': 'test2',
'neighbourhood': 'test3'
}
}
},
{
'type': 'Feature',
@ -198,7 +194,6 @@ module.exports.tests.search = function(test, common) {
]
},
'properties': {
'geocoding': {
'id': '34633854',
'layer': 'venue',
'source': 'osm',
@ -215,7 +210,6 @@ module.exports.tests.search = function(test, common) {
'category': ['tourism', 'transport']
}
}
}
]
};

2
test/unit/sanitiser/reverse.js

@ -133,7 +133,7 @@ module.exports.tests.sanitize_private = function(test, common) {
});
});
var valid_values = ['true', true, 1];
var valid_values = ['true', true, 1, '1'];
valid_values.forEach(function(value) {
test('valid private param ' + value, function(t) {
sanitize({ 'point.lat': 0, 'point.lon': 0, 'private': value }, function( err, clean ){

Loading…
Cancel
Save