roc
Version:
Build modern web applications easily
128 lines (108 loc) • 4.65 kB
JavaScript
;
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; };
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.isValid = isValid;
exports.validate = validate;
exports.validateMightThrow = validateMightThrow;
exports.throwError = throwError;
var _chalk = require('chalk');
var _chalk2 = _interopRequireDefault(_chalk);
var _lodash = require('lodash');
var _style = require('../helpers/style');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* 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 we have no validator we assume the value to be valid
if (!validator) {
return true;
}
if ((0, _lodash.isFunction)(validator)) {
return validator(value);
} else if ((0, _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) {
let metaSettings = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
let toValidate = arguments.length <= 2 || arguments[2] === undefined ? true : arguments[2];
try {
if (toValidate === true) {
validateMightThrow(settings, metaSettings.validations);
} else {
toValidate.forEach(group => {
validateMightThrow(settings[group], metaSettings.validations && metaSettings.validations[group]);
});
}
} catch (err) {
console.log((0, _style.feedbackMessage)((0, _style.errorLabel)('Error', 'Validation Problem'), 'Configuration was not valid.\n\n' + err.message));
/* eslint-disable no-process-exit */
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
const validateKeys = Object.keys(validations);
for (const validateKey of validateKeys) {
const configValue = settings[validateKey];
const validator = validations[validateKey];
// process validation nodes recursively
if ((0, _lodash.isPlainObject)(validator) && (0, _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) {
let 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) {
const result = isValid(value, validator);
if (result !== true) {
throwError(validateKey, result, value);
}
}
//# sourceMappingURL=index.js.map