wa-chat-server
Version:
Watson Assistant powered chat server
73 lines (72 loc) • 3.04 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SchemaValidator = void 0;
const SchemaValidationException_1 = require("../exception/SchemaValidationException");
class SchemaValidator {
constructor(services) {
this.services = services;
}
loadSchemas() {
return __awaiter(this, void 0, void 0, function* () {
this.obligatorySchemas = this.services.getObligatorySchemas();
this.optionalSchemas = this.services.getOptionalSchemas();
this.schemasOnFS = yield this.services.getSchemasOnFS();
});
}
checkForMissingSchemas() {
for (const schema of this.obligatorySchemas) {
if (!this.schemasOnFS.has(schema)) {
this.errors.push(`Schema ${schema} not found!`);
}
}
}
checkForExtraSchemas() {
for (const schema of this.schemasOnFS) {
if (!this.obligatorySchemas.has(schema) && !this.optionalSchemas.has(schema)) {
const str = `Schema ${schema} not used by any conversation service!`;
this.errors.push(str);
}
}
}
validateSchemas() {
return __awaiter(this, void 0, void 0, function* () {
yield Promise.all(this.services.getServices().map((service) => __awaiter(this, void 0, void 0, function* () {
try {
yield service.loadSchemas();
}
catch (e) {
this.errors.push(e.toString().replace(/^Error: /, ''));
}
})));
});
}
/**
* @pre: this.services is populated (done in constructor)
* @post:
* 1) All required JSON schemas are found
* 2) There are no unused JSON schemas on the filesystem
* 3) All JSON schemas are valid
*/
validate() {
return __awaiter(this, void 0, void 0, function* () {
this.errors = [];
yield this.loadSchemas();
this.checkForMissingSchemas();
this.checkForExtraSchemas();
yield this.validateSchemas();
if (this.errors.length > 0) {
throw new SchemaValidationException_1.SchemaValidationException(this.errors.join('\n'));
}
});
}
}
exports.SchemaValidator = SchemaValidator;