roc
Version:
Build modern web applications easily
142 lines (114 loc) • 4.94 kB
JavaScript
;
exports.__esModule = true;
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
exports.isValid = isValid;
exports.validate = validate;
exports.validateMightThrow = validateMightThrow;
exports.throwError = throwError;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
require('source-map-support/register');
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var _lodash = require('lodash');
var _helpersStyle = require('../helpers/style');
/**
* Helper to use a validator.
*
* @param {object} value - Something to validate.
* @param {function|RegExp} validator - A validator.
* @return {boolean} - If valid or not.
*/
function isValid(value, validator) {
if (_lodash.isFunction(validator)) {
return validator(value);
} else if (_lodash.isRegExp(validator)) {
if (!validator.test(value.toString())) {
return 'Did not match the regexp: ' + validator;
}
return true;
}
throw new Error('Structure of configuration does not align with validation.');
}
/**
* Validates the provided configuration object.
*
* @param {rocSettings} settings - The settings object to validate.
* @param {rocMetaSettings} metaSettings - The meta settings object that has information about how to validate.
* @param {array|boolean} toValidate - What groups on settings that should be validated.
* @emits {process.exit} - If the config was invalid it will print the reason and terminate with status 1.
*/
function validate(settings) {
var metaSettings = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var toValidate = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
try {
if (toValidate === true) {
validateMightThrow(settings, metaSettings.validations);
} else {
toValidate.forEach(function (group) {
validateMightThrow(settings[group], metaSettings.validations && metaSettings.validations[group]);
});
}
} catch (err) {
/* eslint-disable no-process-exit, no-console */
console.log(_helpersStyle.errorLabel('Validation problem') + ' Configuration was not valid.\n');
console.log(err.message);
process.exit(1);
/* eslint-enable */
}
}
/**
* Validates the provided configuration object.
*
* @param {rocSettings} settings - The settings object to validate.
* @param {Object} validations - The meta configuration object that has information about how to validate.
* @throws {Error} throws error if the configuration is invalid
*/
function validateMightThrow(settings, validations) {
// If no meta configuration or validation is provided it is valid
if (!validations) {
return;
}
// validation fields to process one by one
var validateKeys = Object.keys(validations);
for (var _iterator = validateKeys, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref;
if (_isArray) {
if (_i >= _iterator.length) break;
_ref = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref = _i.value;
}
var validateKey = _ref;
var configValue = settings[validateKey];
var validator = validations[validateKey];
// process validation nodes recursively
if (_lodash.isPlainObject(validator) && _lodash.isPlainObject(configValue)) {
validateMightThrow(configValue, _extends({}, validator));
} else {
assertValid(configValue, validateKey, validator);
}
}
}
/**
* Throws error for failed validations.
*
* @param {string} name - String with the name of what failed the validation.
* @param {string} message - Potential message from the validating function.
* @param {object} value - The value that was provided.
* @param {string} [type='field'] - What the failed validation value was.
* @throws {Error} - Throws error if the configuration is invalid.
*/
function throwError(name, message, value) {
var type = arguments.length <= 3 || arguments[3] === undefined ? 'field' : arguments[3];
value = value || '[Nothing]';
throw new Error('Validation failed for ' + type + ' ' + _chalk2['default'].underline(name) + ' -' + (' Received: ' + value + '.') + (' ' + (message || '')));
}
function assertValid(value, validateKey, validator) {
var result = isValid(value, validator);
if (result !== true) {
throwError(validateKey, result, value);
}
}
//# sourceMappingURL=index.js.map