UNPKG

@adpt/cli

Version:
126 lines 3.85 kB
"use strict"; /* * Copyright 2020 Unbounded Systems, LLC * * 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 }); const utils_1 = require("@adpt/utils"); const ts_custom_error_1 = require("ts-custom-error"); const types_1 = require("./types"); // tslint:disable-next-line: no-var-requires const parseDuration = require("parse-duration"); class InvalidValue extends ts_custom_error_1.CustomError { } class SchemaValidationError extends ts_custom_error_1.CustomError { constructor(prop, expectedType, actualType) { super(`Validation failed for property '${prop}'. Expected type '${expectedType}'.`); this.prop = prop; this.expectedType = expectedType; this.actualType = actualType; } } exports.SchemaValidationError = SchemaValidationError; function validateBoolean(val) { switch (typeof val) { case "boolean": return val; case "string": switch (val.toLowerCase()) { case "0": case "false": case "off": case "no": return false; case "1": case "true": case "on": case "yes": return true; } } throw new InvalidValue(); } function validateDuration(val) { switch (typeof val) { case "string": const duration = parseDuration(val); if (duration === null) throw new InvalidValue(); return duration; case "number": return val; default: throw new InvalidValue(); } } function validateString(val) { switch (typeof val) { case "string": return val; default: throw new InvalidValue(); } } function storeInput(_vtEntry, orig) { return orig; } function storeOutput(vtEntry, orig) { return vtEntry.validator(orig); } /** * Defines the types we know how to parse and validate. */ exports.valTypeInfo = { boolean: { inputType: types_1.TBoolean, outputType: types_1.TBoolean, validator: validateBoolean, storeFormat: storeOutput, }, duration: { inputType: types_1.TDuration, outputType: types_1.TNumber, validator: validateDuration, storeFormat: storeInput, }, string: { inputType: types_1.TString, outputType: types_1.TString, validator: validateString, storeFormat: storeInput, }, }; function parseItem(prop, val, schema) { if (!(prop in schema)) throw new Error(`Property '${prop}' is not a valid property name`); const asType = schema[prop].asType; const vti = exports.valTypeInfo[asType]; if (!vti) throw new utils_1.InternalError(`Unhandled type '${asType}' in config schema`); const { storeFormat, validator } = vti; try { const parsed = validator(val); const store = storeFormat(vti, val); return { parsed, store, }; } catch (err) { if (err.name !== "InvalidValue") throw err; throw new SchemaValidationError(prop.toString(), asType, typeof val); } } exports.parseItem = parseItem; //# sourceMappingURL=schema.js.map