Gitter migration: Setup redirects (rollout pt. 3)
[gitter.git] / server / utils / options-validator.js
blobc1969cccd59981131da1c4cedc31d544d7a24e63
1 'use strict';
3 const logger = require('gitter-web-env').logger;
5 /**
6 * createOptionsValidator creates a function that will accept options and
7 * log out all unexpected key-value pairs in those options
9 * @param {String} validatorName unique name for identifying log messages
10 * @param {Map<String,Boolean>} expectedOptionNames allowed options as keys with true as value,
11 * all other options are going to be warnings in logs
13 * @example
14 * createOptionsValidator(
15 * 'restful.serializeChatsForTroupe',
16 * { limit: true }
17 * )(options)
19 const createOptionsValidator = (validatorName, expectedOptionNames) => options => {
20 const allOptionNames = Object.keys(options);
21 const unexpectedOptionNames = allOptionNames.filter(
22 optionName => !expectedOptionNames[optionName]
25 if (unexpectedOptionNames.length === 0) {
26 return;
29 const unexpectedOptions = unexpectedOptionNames.reduce(
30 (partialOptions, optionName) => ({
31 ...partialOptions,
32 [optionName]: options[optionName]
33 }),
36 logger.warn(`unexpected options - ${validatorName} - ${JSON.stringify(unexpectedOptions)}`);
39 module.exports = { createOptionsValidator };