curli-config
Version:
A small library to load/validate configuration files placed in different sides of the application using environments
31 lines (30 loc) • 1.15 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const SchemaLoader_1 = require("./SchemaLoader");
const Ajv = require("ajv");
class ValidateUsingSchema {
constructor(toValidate, pathSchemaIs) {
this.toValidate = toValidate;
const schemaLoader = new SchemaLoader_1.SchemaLoader(pathSchemaIs);
this.schema = schemaLoader.loadFile();
this.validate();
}
validate() {
const ajv = new Ajv({ allErrors: true });
const validate = ajv.compile(this.schema);
const valid = validate(this.toValidate);
if (!valid) {
const error = (Array.isArray(validate.errors)) ?
this.concatErrors(validate.errors) : '';
throw new Error('Error validating config schema => ' + error);
}
}
concatErrors(errors) {
let messageToReturn = '';
errors.forEach((error, index) => {
messageToReturn = messageToReturn + ((index >= 1) ? ' | ' : '') + error.message;
});
return messageToReturn;
}
}
exports.ValidateUsingSchema = ValidateUsingSchema;
;