cnabjs
Version:
A library for loading and working with CNAB (Cloud Native Application Bundle) manifests
94 lines • 4.24 kB
JavaScript
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const ajv = __importStar(require("ajv"));
const never_1 = require("./utils/never");
/**
* Provides parameter value validation for a bundle.
*/
var Validator;
(function (Validator) {
/**
* Creates a parameter validator for a CNAB bundle.
* @param bundle The bundle containing parameter declarations and definitions.
* @returns An object which can be used to perform parameter validation
* according to the schema defined in the bundle.
*/
function forBundle(bundle) {
return new ValidatorImpl(bundle);
}
Validator.forBundle = forBundle;
})(Validator = exports.Validator || (exports.Validator = {}));
class ValidatorImpl {
constructor(bundle) {
this.bundle = bundle;
}
validate(parameter, value) {
const schema = this.parameterSchema(parameter);
if (!schema) {
return { isValid: false, reason: 'Bundle does not specify valid parameter values' }; // TODO: more precise error message
}
const validator = ajv.default();
const validate = validator.compile(schema);
const isValid = !!(validate(value)); // We are never async so we can just breeze past the PromiseLike return
if (isValid) {
return { isValid: true };
}
const reason = validate.errors.map((err) => err.message).join(', ');
return { isValid: false, reason: reason };
}
validateText(parameter, valueText) {
const schema = this.parameterSchema(parameter);
if (!schema) {
return { isValid: false, reason: 'Bundle does not specify valid parameter values' }; // TODO: more precise error message
}
const targetType = schema.type || 'string';
switch (targetType) {
case 'string':
return this.validate(parameter, valueText);
case 'integer':
const intValue = Number(valueText);
if (!Number.isInteger(intValue)) {
return { isValid: false, reason: 'The value must be a whole number' };
}
if (valueText.toLowerCase().includes('e')) {
return { isValid: false, reason: 'The value must be a whole number' }; // JS allows scientific notation but CNAB tools are not likely to accept it
}
return this.validate(parameter, intValue);
case 'number':
const floatValue = Number(valueText);
if (isNaN(floatValue)) {
return { isValid: false, reason: 'The value must be a number' };
}
if (valueText.toLowerCase().includes('e')) {
return { isValid: false, reason: 'The value must be a number' }; // JS allows scientific notation but CNAB tools are not likely to accept it
}
return this.validate(parameter, floatValue);
case 'boolean':
const boolValue = valueText.toLowerCase() === 'true' ? true : (valueText.toLowerCase() === 'false' ? false : undefined);
if (boolValue === undefined) {
return { isValid: false, reason: 'The value must be either "true" or "false"' };
}
return this.validate(parameter, boolValue);
}
return never_1.cantHappen(targetType);
}
parameterSchema(parameter) {
if (!this.bundle.parameters || !this.bundle.definitions) {
return undefined;
}
const parameterInfo = this.bundle.parameters[parameter];
if (!parameterInfo) {
return undefined;
}
const definition = this.bundle.definitions[parameterInfo.definition];
return definition;
}
}
//# sourceMappingURL=validation.js.map