Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding addendum namespaces config #140

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions config/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"indexName": "pelias",
"typeName": "_doc"
},
"addendum_namespaces": {},
"logger": {
"level": "debug",
"timestamp": true,
Expand Down
10 changes: 6 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
const fs = require('fs');
const path = require('path');
const _ = require('lodash');

const Joi = require('@hapi/joi');
const global_schema = require('./schema');

const default_config = require( __dirname + '/config/defaults.json' );
let localpath = process.env.HOME + '/pelias.json'; // default location of pelias.json
Expand All @@ -23,15 +22,18 @@ function generate( schema, deep ){

const config = getConfig(deep);

if (_.isObject(schema)) {
// Pelias-config is always an object, so we don't expect
// any other joi schema type.
if (_.isObject(schema) && schema.type === 'object') {
return getValidatedSchema(config, schema);
}

return config;
}

function getValidatedSchema(config, schema) {
const validationResult = schema.validate(config);
const commonSchema = global_schema.concat(schema);
const validationResult = commonSchema.validate(config);

if (validationResult.error) {
throw new Error(validationResult.error.details[0].message);
Expand Down
9 changes: 9 additions & 0 deletions schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const Joi = require('@hapi/joi');

module.exports = Joi.object().keys({
addendum_namespaces: Joi.object().pattern(
Joi.string().min(2), Joi.object().required().keys({
type: Joi.string().valid('array', 'number', 'string', 'boolean').required()
})
)
}).unknown(true);
1 change: 1 addition & 0 deletions test/expected-deep.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"indexName": "pelias",
"typeName": "_doc"
},
"addendum_namespaces": {},
"logger": {
"level": "debug",
"timestamp": true,
Expand Down
13 changes: 2 additions & 11 deletions test/generate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

const path = require('path');
const config = require('../');
const defaults = require('../config/defaults');
Expand Down Expand Up @@ -212,14 +211,6 @@ module.exports.generate.paths = function(test) {
};

module.exports.generate.validate = (test) => {
test('non-validating schema should throw an error', (t) => {
t.throws(() => {
config.generate(Joi.boolean());
}, /"value" must be a boolean/);
t.end();

});

test('validating schema should not throw an error', (t) => {
const schema = Joi.object().keys({
imports: Joi.object()
Expand Down Expand Up @@ -284,7 +275,7 @@ module.exports.generate.validate = (test) => {
t.end();
});

test('generateDefaults returns default config always', function(t) {
test('generateDefaults returns default config always', (t) => {
// set the PELIAS_CONFIG env var, this config should NOT be used
process.env.PELIAS_CONFIG = path.resolve( __dirname + '/../config/env.json' );

Expand All @@ -297,7 +288,7 @@ module.exports.generate.validate = (test) => {
delete process.env.PELIAS_CONFIG;
});

test('generateCustom returns defaults with custom settings overridden', function(t) {
test('generateCustom returns defaults with custom settings overridden', (t) => {
const custom_config = {
api: {
customValue: 1, //add a new setting
Expand Down
3 changes: 2 additions & 1 deletion test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ var common = {};

var tests = [
require('./interface.js'),
require('./generate.js')
require('./generate.js'),
require('./schema.js')
];

tests.map(function(t) {
Expand Down
126 changes: 126 additions & 0 deletions test/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const schema = require('../schema');

module.exports.tests = {};

module.exports.tests.addendum_namespaces_validation = (test) => {
test( 'addendum_namespaces should be of object type', (t) => {
const config = {
addendum_namespaces: {
tariff_zone_ids: '123'
}
};

const result = schema.validate(config);

t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"addendum_namespaces.tariff_zone_ids" must be of type object');
t.end();

});

test( 'addendum_namespaces of type other than array, string , boolean and number, should not be acceptable', (t) => {
const config = {
addendum_namespaces: {
tariff_zone_ids: {
type: 'object'
}
}
};

const result = schema.validate(config);

t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"addendum_namespaces.tariff_zone_ids.type" must be one of [array, number, string, boolean]');
t.end();

});

test( 'addendum_namespaces name should be at least 2 characters', (t) => {
const config = {
addendum_namespaces: {
t: {
type: 'object'
}
}
};

const result = schema.validate(config);

t.equals(result.error.details.length, 1);
t.equals(result.error.details[0].message, '"addendum_namespaces.t" is not allowed');
t.end();
});

test( 'addendum_namespaces of type array should be acceptable', (t) => {
const config = {
addendum_namespaces: {
tariff_zone_ids: {
type: 'array'
}
}
};

const result = schema.validate(config);

t.notOk(result.error);
t.end();

});

test( 'addendum_namespaces of type string should be acceptable', (t) => {
const config = {
addendum_namespaces: {
tariff_zone_ids: {
type: 'string'
}
}
};

const result = schema.validate(config);

t.notOk(result.error);
t.end();
});

test( 'addendum_namespaces of type number should be acceptable', function(t) {
const config = {
addendum_namespaces: {
tariff_zone_ids: {
type: 'number'
}
}
};

const result = schema.validate(config);

t.notOk(result.error);
t.end();
});

test( 'addendum_namespaces of type boolean should be acceptable', function(t) {
const config = {
addendum_namespaces: {
tariff_zone_ids: {
type: 'boolean'
}
}
};

const result = schema.validate(config);

t.notOk(result.error);
t.end();
});

};

module.exports.all = (tape, common) => {

function test(name, testFunction) {
return tape('configValidation: ' + name, testFunction);
}

for( var testCase in module.exports.tests ){
module.exports.tests[testCase](test, common);
}
};