recime-bot-runtime
Version:
This runtime is intended to run inside a micro-service container with platform specific integration and module interpreter.
73 lines (72 loc) • 3.48 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
var validator_1 = require("./validator");
var StringValidator = /** @class */ (function (_super) {
__extends(StringValidator, _super);
function StringValidator() {
return _super !== null && _super.apply(this, arguments) || this;
}
StringValidator.prototype.validate = function (__, vars, args, data) {
var _this = this;
var input = args.text;
var message = data.response[data.index];
var content = message.content;
return new Promise(function (resolve, reject) {
if (content.validations) {
for (var _i = 0, _a = content.validations; _i < _a.length; _i++) {
var validation = _a[_i];
switch (validation.name) {
case 'email': {
if (!input.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ig))
return reject(__.text(validation.message));
break;
}
case 'url': {
if (!input.match(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/ig))
return reject(__.text(validation.message));
break;
}
case 'pattern': {
try {
if (!input.match(new RegExp(validation.expression, "ig"))) {
return reject(__.text(validation.message));
}
}
catch (ex) {
delete vars['user-input'];
return reject(__.text(ex.message));
}
break;
}
case 'minLength': {
if (!input || input.length < validation.length)
return reject(__.text(validation.message.replace(/\{0\}/ig, validation.length)));
break;
}
case 'maxLength': {
if (input && input.length > validation.length)
return reject(__.text(validation.message.replace(/\{0\}/ig, validation.length)));
break;
}
}
}
}
return vars.post(content.variable.replace(/[{}]+/ig, ""), input).then(function () {
delete vars.values['user-input'];
return _this.responder.processResponse(data, vars);
}).then(resolve);
});
};
return StringValidator;
}(validator_1.Validator));
exports.StringValidator = StringValidator;