@atomist/sdm
Version:
Atomist Software Delivery Machine SDK
76 lines • 3.16 kB
JavaScript
;
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateConfigurationValues = exports.ConfigurationValueType = void 0;
const _ = require("lodash");
/**
* Describe the type of configuration value
*/
var ConfigurationValueType;
(function (ConfigurationValueType) {
ConfigurationValueType[ConfigurationValueType["Number"] = 0] = "Number";
ConfigurationValueType[ConfigurationValueType["String"] = 1] = "String";
ConfigurationValueType[ConfigurationValueType["Boolean"] = 2] = "Boolean";
})(ConfigurationValueType = exports.ConfigurationValueType || (exports.ConfigurationValueType = {}));
/**
* Validate the provided configuration
* @param config
* @param options
*/
function validateConfigurationValues(config, options) {
const missingValues = [];
const invalidValues = [];
(options.requiredConfigurationValues || []).forEach(v => {
const path = typeof v === "string" ? v : v.path;
const type = typeof v === "string" ? ConfigurationValueType.String : v.type;
const value = _.get(config, path);
if (!value) {
missingValues.push(path);
}
else {
switch (type) {
case ConfigurationValueType.Number:
if (typeof value !== "number") {
invalidValues.push(`${path} ${JSON.stringify(value)} is not a 'number'`);
}
break;
case ConfigurationValueType.String:
if (typeof value !== "string") {
invalidValues.push(`${path} ${JSON.stringify(value)} is not a 'string'`);
}
break;
case ConfigurationValueType.Boolean:
if (typeof value !== "boolean") {
invalidValues.push(`${path} ${JSON.stringify(value)} is not a 'boolean'`);
}
break;
}
}
});
const errors = [];
if (missingValues.length > 0) {
errors.push(`Missing configuration values. Please add the following values to your client configuration: '${missingValues.join(", ")}'`);
}
if (invalidValues.length > 0) {
errors.push(`Invalid configuration values. The following values have the wrong type: '${invalidValues.join(", ")}'`);
}
if (errors.length > 0) {
throw new Error(errors.join("\n"));
}
}
exports.validateConfigurationValues = validateConfigurationValues;
//# sourceMappingURL=ConfigurationValues.js.map