jaywalk
Version:
Runtime type validation
48 lines • 1.73 kB
JavaScript
;
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var string_1 = require('./string');
var utils_1 = require('../utils');
var Pattern = (function (_super) {
__extends(Pattern, _super);
function Pattern(options) {
_super.call(this, options);
this.type = 'Email';
this.pattern = options.pattern;
this._regexp = new RegExp(this.pattern);
this._tests.push(toPatternTest(this.pattern, this._regexp));
}
Pattern.prototype._isType = function (value, path, context) {
var _this = this;
return utils_1.wrapIsType(this, value, path, context, _super.prototype._isType, function (value) {
if (_this._regexp.test(value)) {
return 1;
}
throw context.error(path, 'Pattern', 'pattern', _this.pattern, value);
});
};
Pattern.prototype._extend = function (options) {
var res = _super.prototype._extend.call(this, options);
delete res._regexp;
return res;
};
Pattern.prototype.toJSON = function () {
var json = _super.prototype.toJSON.call(this);
delete json._regexp;
return json;
};
return Pattern;
}(string_1.String));
exports.Pattern = Pattern;
function toPatternTest(pattern, re) {
return function (value, path, context, next) {
if (!re.test(value)) {
throw context.error(path, 'Pattern', 'pattern', pattern, value);
}
return next(value);
};
}
//# sourceMappingURL=pattern.js.map