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

Simplify the default value generation for all configurations - Closes #3273 #3288

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 7 additions & 5 deletions framework/src/components/cache/defaults/config.js
Expand Up @@ -3,37 +3,39 @@ const defaultConfig = {
properties: {
enabled: {
type: 'boolean',
default: false,
env: 'LISK_CACHE_ENABLED',
},
host: {
type: 'string',
format: 'ipOrFQDN',
default: '127.0.0.1',
env: 'LISK_REDIS_HOST',
arg: '-r,--redis',
},
port: {
type: 'integer',
minimum: 1,
maximum: 65535,
default: 6380,
env: 'LISK_REDIS_PORT',
},
db: {
type: 'integer',
minimum: 0,
maximum: 15,
default: 0,
env: 'LISK_REDIS_DB_NAME',
},
password: {
type: ['string', 'null'],
default: null,
env: 'LISK_REDIS_DB_PASSWORD',
},
},
required: ['enabled', 'host', 'port', 'db', 'password'],
default: {
enabled: false,
host: '127.0.0.1',
port: 6380,
db: 0,
nazarhussain marked this conversation as resolved.
Show resolved Hide resolved
password: null,
nazarhussain marked this conversation as resolved.
Show resolved Hide resolved
},
};

module.exports = defaultConfig;
2 changes: 1 addition & 1 deletion framework/src/components/cache/index.js
Expand Up @@ -36,7 +36,7 @@ const { config: defaultConfig } = require('./defaults');
const validator = require('../../controller/helpers/validator');

function createCacheComponent(options, logger) {
const optionsWithDefaults = validator.validateWithDefaults(
const optionsWithDefaults = validator.parseEnvArgAndValidate(
defaultConfig,
options
);
Expand Down
8 changes: 5 additions & 3 deletions framework/src/components/logger/defaults/config.js
Expand Up @@ -3,24 +3,26 @@ const defaultConfig = {
properties: {
fileLogLevel: {
type: 'string',
default: 'info',
enum: ['trace', 'debug', 'log', 'info', 'warn', 'error', 'fatal', 'none'],
env: 'LISK_FILE_LOG_LEVEL',
arg: '-l,--log',
},
logFileName: {
type: 'string',
default: 'logs/lisk.log',
env: 'LISK_REDIS_HOST',
},
consoleLogLevel: {
type: 'string',
default: 'none',
enum: ['trace', 'debug', 'log', 'info', 'warn', 'error', 'fatal', 'none'],
env: 'LISK_CONSOLE_LOG_LEVEL',
},
},
required: ['fileLogLevel', 'logFileName', 'consoleLogLevel'],
default: {
fileLogLevel: 'info',
consoleLogLevel: 'none',
logFileName: 'logs/lisk.log',
},
};

module.exports = defaultConfig;
2 changes: 1 addition & 1 deletion framework/src/components/logger/index.js
Expand Up @@ -19,7 +19,7 @@ const { config: defaultConfig } = require('./defaults');
const validator = require('../../controller/helpers/validator');

function createLoggerComponent(options = {}) {
const optionsWithDefaults = validator.validateWithDefaults(
const optionsWithDefaults = validator.parseEnvArgAndValidate(
defaultConfig,
options
);
Expand Down
24 changes: 13 additions & 11 deletions framework/src/components/storage/defaults/config.js
Expand Up @@ -3,58 +3,47 @@ const defaultConfig = {
properties: {
host: {
type: 'string',
default: 'localhost',
env: 'LISK_DB_HOST',
},
port: {
type: 'integer',
minimum: 1,
maximum: 65535,
default: 5432,
env: 'LISK_DB_PORT',
},
database: {
type: 'string',
default: '',
env: 'LISK_DB_NAME',
arg: '-d,--database',
},
user: {
type: 'string',
default: 'lisk',
env: 'LISK_DB_USER',
},
password: {
type: 'string',
default: 'password',
env: 'LISK_DB_PASSWORD',
},
min: {
type: 'integer',
default: 10,
},
max: {
type: 'integer',
default: 95,
},
poolIdleTimeout: {
type: 'integer',
default: 30000,
},
reapIntervalMillis: {
type: 'integer',
default: 1000,
},
logEvents: {
type: 'array',
items: {
type: 'string',
},
default: ['error'],
},
logFileName: {
type: 'string',
default: 'logs/lisk_db.log',
},
},
required: [
Expand All @@ -69,6 +58,19 @@ const defaultConfig = {
'reapIntervalMillis',
'logEvents',
],
default: {
host: 'localhost',
port: 5432,
database: '',
user: 'lisk',
password: 'password',
min: 10,
max: 95,
poolIdleTimeout: 30000,
reapIntervalMillis: 1000,
logEvents: ['error'],
logFileName: 'logs/lisk_db.log',
},
};

module.exports = defaultConfig;
2 changes: 1 addition & 1 deletion framework/src/components/storage/index.js
Expand Up @@ -34,7 +34,7 @@ if (process.env.NEW_RELIC_LICENSE_KEY) {
}

function createStorageComponent(options, logger) {
options = validator.validateWithDefaults(defaultConfig, options);
options = validator.parseEnvArgAndValidate(defaultConfig, options);

const storage = new Storage(options, logger);

Expand Down
4 changes: 2 additions & 2 deletions framework/src/controller/application.js
Expand Up @@ -122,7 +122,7 @@ class Application {
validator.loadSchema(constantsSchema);
validator.validate(applicationSchema.appLabel, appLabel);
validator.validate(applicationSchema.config, appConfig);
constants = validator.validateWithDefaults(
constants = validator.parseEnvArgAndValidate(
constantsSchema.constants,
constants
);
Expand Down Expand Up @@ -332,7 +332,7 @@ class Application {

Object.keys(modules).forEach(alias => {
this.logger.info(`Validating module options with alias: ${alias}`);
this.config.modules[alias] = validator.validateWithDefaults(
this.config.modules[alias] = validator.parseEnvArgAndValidate(
modules[alias].defaults,
this.config.modules[alias]
);
Expand Down
26 changes: 15 additions & 11 deletions framework/src/controller/helpers/validator/index.js
Expand Up @@ -15,6 +15,7 @@
'use strict';

const assert = require('assert');
const _ = require('lodash');

/**
* Custom Lisk Framework Validator implemented on top of Ajv (https://github.com/epoberezkin/ajv)
Expand All @@ -32,22 +33,22 @@ const validator = new Ajv({
$data: true,
});

const validatorWithDefaults = new Ajv({
const parserAndValidator = new Ajv({
allErrors: true,
schemaId: 'auto',
useDefaults: true,
useDefaults: false,
$data: true,
});

validatorWithDefaults.addKeyword('env', envKeyword);
validatorWithDefaults.addKeyword('arg', argKeyword);
parserAndValidator.addKeyword('env', envKeyword);
parserAndValidator.addKeyword('arg', argKeyword);

Object.keys(formats).forEach(formatId => {
validator.addFormat(formatId, formats[formatId]);
});

Object.keys(formats).forEach(formatId => {
validatorWithDefaults.addFormat(formatId, formats[formatId]);
parserAndValidator.addFormat(formatId, formats[formatId]);
});

/**
Expand All @@ -67,7 +68,7 @@ const validatorInterface = {
});

Object.keys(schema).forEach(key => {
validatorWithDefaults.addSchema(schema[key], schema[key].id);
parserAndValidator.addSchema(schema[key], schema[key].id);
});
},

Expand All @@ -92,14 +93,17 @@ const validatorInterface = {
*
* @param {Object} schema - JSON Schema object
* @param {Object} data - Data object you want to validate
* @param {Object} defaultValues - Default values you want to use
* @return {Object} - Modified data with default values
* @throws Framework.errors.SchemaValidationError
*/
validateWithDefaults: (schema, data) => {
const dataCopy = { ...data };
parseEnvArgAndValidate: (schema, data, defaultValues = schema.default) => {
const dataCopy = defaultValues
? _.defaultsDeep(data, defaultValues)
: { ...data };

if (!validatorWithDefaults.validate(schema, dataCopy)) {
throw new SchemaValidationError(validatorWithDefaults.errors);
if (!parserAndValidator.validate(schema, dataCopy)) {
throw new SchemaValidationError(parserAndValidator.errors);
}

return dataCopy;
Expand Down Expand Up @@ -129,7 +133,7 @@ const validatorInterface = {
},

validator,
validatorWithDefaults,
parserAndValidator,

// TODO: Old interface for validation, must be removed.
ZSchema,
Expand Down
6 changes: 3 additions & 3 deletions framework/src/controller/schema/application.js
Expand Up @@ -67,7 +67,7 @@ module.exports = {
transactions: {
type: 'array',
items: {
$ref: 'transactions',
$ref: 'genesisTransactions',
},
uniqueItems: true,
},
Expand All @@ -89,8 +89,8 @@ module.exports = {
additionalProperties: false,
},

transactions: {
id: 'transactions',
genesisTransactions: {
id: 'genesisTransactions',
type: 'object',
required: ['type', 'timestamp', 'senderPublicKey', 'signature'],
properties: {
Expand Down
4 changes: 3 additions & 1 deletion framework/src/controller/schema/config.js
Expand Up @@ -57,13 +57,15 @@ module.exports = {
properties: {
enabled: {
type: 'boolean',
default: false,
},
},
},
},
required: ['modules', 'components'],
additionalProperties: false,
default: {
ipc: false,
},
},

moduleConfig: {
Expand Down