nowjs-core
Version:
NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence
36 lines (35 loc) • 1.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const regx = /(\$\{([a-zA-Z0-9_\-\@\$\#\!\~\&\^\%\*]+)\})/gim;
class ValidatorBase {
constructor(name, errorMessage) {
this.name = name;
this.errorMessage = errorMessage;
}
get Name() {
return this.name;
}
get ErrorFormat() {
if (typeof this.errorMessage === 'string') {
return this.errorMessage;
}
else if (typeof this.errorMessage === 'function') {
return this.errorMessage(this.name);
}
else {
return '';
}
}
formatErrorMessage(context) {
let message = this.ErrorFormat;
const that = this;
let matches = [];
while ((matches = regx.exec(message))) {
const value = that[matches[2]] ? that[matches[2]] : context[matches[2]];
message = message.replace(matches[1], value ? value : '');
regx.lastIndex = 0;
}
return message;
}
}
exports.ValidatorBase = ValidatorBase;