Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ The API recognizes the following properties under the top-level `api` key in you
|`accessLog`|*no*||name of the format to use for access logs; may be any one of the [predefined values](https://github.com/expressjs/morgan#predefined-formats) in the `morgan` package. Defaults to `"common"`; if set to `false`, or an otherwise falsy value, disables access-logging entirely.|
|`relativeScores`|*no*|true|if set to true, confidence scores will be normalized, realistically at this point setting this to false is not tested or desirable
|`exposeInternalDebugTools`|*no*|true|Exposes several debugging tools, such as the ability to enable Elasticsearch explain mode, that may come at a performance cost or expose sensitive infrastructure details. Not recommended if the Pelias API is open to the public.
|`minSize`|*no*|1| Allows configuring the minimum number of results a query can request.
|`maxSize`|*no*|40| Allows configuring the maximum number of results a query can request.
|`defaultSize`|*no*|10| Allows configuring the number of results a query will request when a size wasn't specified. Must be greater than or equal to `minSize` and smaller than or equal to `maxSize`.

A good starting configuration file includes this section (fill in the service and Elasticsearch hosts as needed):

Expand Down
9 changes: 6 additions & 3 deletions sanitizer/_size.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
const _ = require('lodash');
const MIN_SIZE = 1;
const MAX_SIZE = 40;
const DEFAULT_SIZE = 10;
const peliasConfig = require('pelias-config').generate();

// Allow custom min, max and default sizes
const MIN_SIZE = _.get(peliasConfig, 'api.minSize', 1);
const MAX_SIZE = _.get(peliasConfig, 'api.maxSize', 40);
const DEFAULT_SIZE = _.clamp(_.get(peliasConfig, 'api.defaultSize', 10), MIN_SIZE, MAX_SIZE);

// validate inputs, convert types and apply defaults
function _setup( size_min, size_max, size_def ){
Expand Down
6 changes: 6 additions & 0 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ const Joi = require('@hapi/joi');
// * api.relativeScores (boolean)
// * api.exposeInternalDebugTools (boolean)
// * api.localization (flipNumberAndStreetCountries is array of 3 character strings)
// * api.maxSize (int)
// * api.minSize (int)
// * api.defaultSize (int)
module.exports = Joi.object().keys({
api: Joi.object().required().keys({
version: Joi.string(),
Expand All @@ -26,6 +29,9 @@ module.exports = Joi.object().keys({
layer: Joi.object(),
source: Joi.object()
}),
minSize: Joi.number().integer().min(1).max(Joi.ref('maxSize')).default(1),
maxSize: Joi.number().integer().min(1).default(40),
defaultSize: Joi.number().min(Joi.ref('minSize')).max(Joi.ref('maxSize')).default(10),
localization: Joi.object().keys({
flipNumberAndStreetCountries: Joi.array().items(Joi.string().regex(/^[A-Z]{3}$/))
}).unknown(false),
Expand Down
92 changes: 90 additions & 2 deletions test/unit/schema.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Joi = require('@hapi/joi');
const schema = require('../../schema');
const _ = require('lodash');

Expand Down Expand Up @@ -34,7 +33,10 @@ module.exports.tests.completely_valid = (test, common) => {
defaultParameters: {
'focus.point.lat': 19,
'focus.point.lon': 91
}
},
minSize: 1,
maxSize: 40,
defaultSize: 10
},
esclient: {
requestTimeout: 17
Expand Down Expand Up @@ -517,6 +519,92 @@ module.exports.tests.api_validation = (test, common) => {

});

test('config with non-integer maxSize should throw an error', (t) => {
var config = {
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
maxSize: 'ab'
},
esclient: {
requestTimeout: 17
},
};

const result = schema.validate(config);

t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"api.maxSize" must be a number');
t.end();

});

test('config with a negative minSize should throw an error', (t) => {
var config = {
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
minSize: -1
},
esclient: {
requestTimeout: 17
},
};

const result = schema.validate(config);

t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"api.minSize" must be larger than or equal to 1');
t.end();

});

test('config with a bigger defaultSize than maxSize should throw an error', (t) => {
var config = {
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
maxSize: 5,
defaultSize: 6
},
esclient: {
requestTimeout: 17
},
};

const result = schema.validate(config);

t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"api.defaultSize" must be less than or equal to ref:maxSize');
t.end();

});

test('config with a bigger minSize than maxSize should throw an error', (t) => {
var config = {
api: {
version: 'version value',
indexName: 'index name value',
host: 'host value',
maxSize: 5,
minSize: 6
},
esclient: {
requestTimeout: 17
},
};

const result = schema.validate(config);

t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"api.minSize" must be less than or equal to ref:maxSize');
t.end();

});


};

Expand Down